Boring counting

Problem Description

In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of queries, for each query, please tell us among all the vertices in the subtree rooted at vertice u, how many different kinds of weights appear exactly K times?

给出一根为\(1\)的树,每个点都有权值,每次询问以\(u\)为根的子树中正好出现\(k\)次的权值有几个

权值可以离散化,离散化之后范围在\([1,n]\),然后通过树上启发式合并可以得到所有点的答案,对于每个询问查询答案即可

具体方法就是维护每个权值出现的次数,和每个权值出现的次数的次数

//#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<bits/stdc++.h>
using namespace std;
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int n,k,w[MAXN],ret[MAXN],sz[MAXN],son[MAXN],cnt[MAXN],app[MAXN];
vector<int> G[MAXN];
void dfs(int u, int par){
son[u] = 0; sz[u] = 1;
for(int v : G[u]){
if(v==par) continue;
dfs(v,u);
sz[u] += sz[v];
if(sz[son[u]]<sz[v]) son[u] = v;
}
}
void inc(int x){
cnt[x]++;
app[cnt[x]]++;
app[cnt[x]-1]--;
}
void dec(int x){
cnt[x]--;
app[cnt[x]]++;
app[cnt[x]+1]--;
}
void update(int u, int par, bool add){
if(add) inc(w[u]);
else dec(w[u]);
for(int v : G[u]) if(v!=par) update(v,u,add);
}
void search(int u, int par, bool clear){
for(int v : G[u]) if(v!=par and v!=son[u]) search(v,u,true);
if(son[u]) search(son[u],u,false);
for(int v : G[u]) if(v!=par and v!=son[u]) update(v,u,true);
inc(w[u]);
ret[u] = app[k];
if(clear) update(u,par,false);
}
void solve(int kase){
if(kase!=1) puts("");
scanf("%d %d",&n,&k);
for(int i = 1; i <= n; i++) G[i].clear();
vector<int> vec;
for(int i = 1; i <= n; i++){
scanf("%d",&w[i]);
vec.emplace_back(w[i]);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for(int i = 1; i <= n; i++) w[i] = lower_bound(vec.begin(),vec.end(),w[i]) - vec.begin() + 1;
for(int i = 1; i < n; i++){
int u, v; scanf("%d %d",&u,&v);
G[u].emplace_back(v); G[v].emplace_back(u);
}
dfs(1,0);
search(1,0,true);
printf("Case #%d:\n",kase);
int q; scanf("%d",&q);
while(q--){
int x; scanf("%d",&x);
printf("%d\n",ret[x]);
}
}
int main(){
int T; scanf("%d",&T);
for(int kase = 1; kase <= T; kase++) solve(kase);
return 0;
}

HDU4358 Boring counting【dsu on tree】的更多相关文章

  1. 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...

  2. HDU6504 Problem E. Split The Tree【dsu on tree】

    Problem E. Split The Tree Problem Description You are given a tree with n vertices, numbered from 1 ...

  3. HDU6430 Problem E. TeaTree【dsu on tree】

    Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...

  4. CF 375D. Tree and Queries【莫队 | dsu on tree】

    题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...

  5. HDU6191 Query on A Tre【dsu on tree + 01字典树】

    Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, ...

  6. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  7. 【Balanced Binary Tree】cpp

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  8. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  9. 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】

    平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...

随机推荐

  1. 上班从换一张桌面壁纸开始——开源小工具Bing每日壁纸

    发布一个自用的开源小软件,Bing每日壁纸,使用c# winform开发.该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事. 功能特性 自动获取Bing最新图片 ...

  2. Java 中泛型的实现原理

    泛型是 Java 开发中常用的技术,了解泛型的几种形式和实现泛型的基本原理,有助于写出更优质的代码.本文总结了 Java 泛型的三种形式以及泛型实现原理. 泛型 泛型的本质是对类型进行参数化,在代码逻 ...

  3. Linux下的upx命令学习

    upx学习 今天我们来学习一款给应用加壳的软件,叫做upx(the Ultimate Packer for eXecutables) 首先我们先看下它**百科的释义: UPX (the Ultimat ...

  4. Go GRPC 入门(二)

    前言 最近较忙,其实准备一篇搞定的 中途有事,只能隔了一天再写 正文 pb.go 需要注意的是,在本个 demo 中,客户端与服务端都是 Golang,所以在客户端与服务端都公用一个 pb.go 模板 ...

  5. 一个非常棒的Go-Json解析库

    json是一种数据格式,经常被用作数据交换,页面展示,序列化等场景,基本每种语言都有对应的json解析框架,Go语言也不例外,并且内置了json库,基本能够满足一些普通开发场景,但有些复杂场景下就不太 ...

  6. uni-app开发经验分享十: 封装request请求

    http.js //封装requset,uploadFile和downloadFile请求,新增get和post请求方法 let http = { 'setBaseUrl': (url) => ...

  7. JVM(二)类加载的时机及其过程

    类从被加载到虚拟机内存中开始,到卸载出内存为止,它的的整个生命周期包括: 加载(Loading),验证(Verification),准备(Preparation),解析(Resolution),初始化 ...

  8. (16)-Python3之--集合(set)操作

    1.定义 集合的关键字:set 集合主要作用: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 集合用大括号{}表示,元素间用逗号分隔. 建立集合类型用{} ...

  9. Go Code Review Comments

    Go Code Review Comments https://golang.org/wiki/CodeReviewComments

  10. 源码 redis 分布式锁

    https://github.com/SPSCommerce/redlock-py/tree/master/redlock