HDU4358 Boring counting【dsu on tree】
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】的更多相关文章
- 【DSU on tree】【CF741D】Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
Description 给定一棵 \(n\) 个节点的树,每条边上有一个字符,字符集大小 \(22\),求每个节点的子树内最长的简单路径使得路径上的字符经过重排后构成回文串. Limitation \ ...
- 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 ...
- HDU6430 Problem E. TeaTree【dsu on tree】
Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- 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, ...
- 【Invert Binary Tree】cpp
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...
- 【Balanced Binary Tree】cpp
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: ...
- 【遍历二叉树】10判断二叉树是否平衡【Balanced Binary Tree】
平衡的二叉树的定义都是递归的定义,所以,用递归来解决问题,还是挺容易的额. 本质上是递归的遍历二叉树. ++++++++++++++++++++++++++++++++++++++++++++++++ ...
随机推荐
- PHP 插件资源
PHP jsonRPC 百度云网盘地址 https://pan.baidu.com/s/1itCIhrdd5bPGJMefNUuKvw 提取码 : ax4d PHP Excel 百度云网盘 ...
- C#扫盲篇(四):.NET Core 的异步编程-只讲干货(async,await,Task)
关于async,await,task的用法和解释这里就不要说明了,网上一查一大堆.至于为啥还要写这篇文章,主要是其他文章水分太多,不适合新手学习和理解.以下内容纯属个人理解,如果有误,请高手指正.本文 ...
- Java 中泛型的实现原理
泛型是 Java 开发中常用的技术,了解泛型的几种形式和实现泛型的基本原理,有助于写出更优质的代码.本文总结了 Java 泛型的三种形式以及泛型实现原理. 泛型 泛型的本质是对类型进行参数化,在代码逻 ...
- Flutter 布局类组件:简介
前言 布局类组件都会包含一个或多个子组件,不同的布局类组件对子组件排版(layout)方式不同. 我们知道,Element树才是最终的绘制树,Element树是通过Widget树来创建的(通过Widg ...
- Java内存模型与线程(二)线程的实现和线程的调度
先行先发生原则(happen-before原则) 先行先发生是指Java内存模型中定义的两项操作之间的偏序关系. 如果说A先行于B,其实就是说在发生B操作之前,操作A产生的影响能被操作B观察到,至于这 ...
- 【Python】国内pip节点
pip在国内使用国内节点: http://pypi.douban.com/simple 现在已经无法使用了,新版的python3需要使用https://pypi.douban.com/simple/ ...
- jackson学习之一:基本信息
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 请谨慎使用 avaliable 方法来申请缓冲区
问题 今天开始尝试用 Java 写 http 服务器,开局就遇到 Bug. 我先写了一个多线程的.BIO 的 http 服务器,其中接收请求的部分,会将请求的第一行打印出来. 下面是浏览器发出的请求和 ...
- 说说C# 8.0 新增功能Index和Range的^0是什么?
前言 在<C# 8.0 中使用 Index 和 Range>这篇中有人提出^0是什么意思?处于好奇就去试了,结果抛出异常.查看官方文档说^0索引与 sequence[sequence.Le ...
- PC端微信多开方式.bat(Windows批处理文件)
start 微信安装路径\WeChat.exe start 微信安装路径\WeChat.exe