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]\),然后通过树上启发式合并可以得到所有点的答案,对于每个询问查询答案即可

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

  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MAXN = 1e5+7;
  7. int n,k,w[MAXN],ret[MAXN],sz[MAXN],son[MAXN],cnt[MAXN],app[MAXN];
  8. vector<int> G[MAXN];
  9. void dfs(int u, int par){
  10. son[u] = 0; sz[u] = 1;
  11. for(int v : G[u]){
  12. if(v==par) continue;
  13. dfs(v,u);
  14. sz[u] += sz[v];
  15. if(sz[son[u]]<sz[v]) son[u] = v;
  16. }
  17. }
  18. void inc(int x){
  19. cnt[x]++;
  20. app[cnt[x]]++;
  21. app[cnt[x]-1]--;
  22. }
  23. void dec(int x){
  24. cnt[x]--;
  25. app[cnt[x]]++;
  26. app[cnt[x]+1]--;
  27. }
  28. void update(int u, int par, bool add){
  29. if(add) inc(w[u]);
  30. else dec(w[u]);
  31. for(int v : G[u]) if(v!=par) update(v,u,add);
  32. }
  33. void search(int u, int par, bool clear){
  34. for(int v : G[u]) if(v!=par and v!=son[u]) search(v,u,true);
  35. if(son[u]) search(son[u],u,false);
  36. for(int v : G[u]) if(v!=par and v!=son[u]) update(v,u,true);
  37. inc(w[u]);
  38. ret[u] = app[k];
  39. if(clear) update(u,par,false);
  40. }
  41. void solve(int kase){
  42. if(kase!=1) puts("");
  43. scanf("%d %d",&n,&k);
  44. for(int i = 1; i <= n; i++) G[i].clear();
  45. vector<int> vec;
  46. for(int i = 1; i <= n; i++){
  47. scanf("%d",&w[i]);
  48. vec.emplace_back(w[i]);
  49. }
  50. sort(vec.begin(),vec.end());
  51. vec.erase(unique(vec.begin(),vec.end()),vec.end());
  52. for(int i = 1; i <= n; i++) w[i] = lower_bound(vec.begin(),vec.end(),w[i]) - vec.begin() + 1;
  53. for(int i = 1; i < n; i++){
  54. int u, v; scanf("%d %d",&u,&v);
  55. G[u].emplace_back(v); G[v].emplace_back(u);
  56. }
  57. dfs(1,0);
  58. search(1,0,true);
  59. printf("Case #%d:\n",kase);
  60. int q; scanf("%d",&q);
  61. while(q--){
  62. int x; scanf("%d",&x);
  63. printf("%d\n",ret[x]);
  64. }
  65. }
  66. int main(){
  67. int T; scanf("%d",&T);
  68. for(int kase = 1; kase <= T; kase++) solve(kase);
  69. return 0;
  70. }

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. PHP 插件资源

    PHP   jsonRPC  百度云网盘地址  https://pan.baidu.com/s/1itCIhrdd5bPGJMefNUuKvw   提取码 : ax4d PHP Excel 百度云网盘 ...

  2. C#扫盲篇(四):.NET Core 的异步编程-只讲干货(async,await,Task)

    关于async,await,task的用法和解释这里就不要说明了,网上一查一大堆.至于为啥还要写这篇文章,主要是其他文章水分太多,不适合新手学习和理解.以下内容纯属个人理解,如果有误,请高手指正.本文 ...

  3. Java 中泛型的实现原理

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

  4. Flutter 布局类组件:简介

    前言 布局类组件都会包含一个或多个子组件,不同的布局类组件对子组件排版(layout)方式不同. 我们知道,Element树才是最终的绘制树,Element树是通过Widget树来创建的(通过Widg ...

  5. Java内存模型与线程(二)线程的实现和线程的调度

    先行先发生原则(happen-before原则) 先行先发生是指Java内存模型中定义的两项操作之间的偏序关系. 如果说A先行于B,其实就是说在发生B操作之前,操作A产生的影响能被操作B观察到,至于这 ...

  6. 【Python】国内pip节点

    pip在国内使用国内节点: http://pypi.douban.com/simple 现在已经无法使用了,新版的python3需要使用https://pypi.douban.com/simple/ ...

  7. jackson学习之一:基本信息

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. 请谨慎使用 avaliable 方法来申请缓冲区

    问题 今天开始尝试用 Java 写 http 服务器,开局就遇到 Bug. 我先写了一个多线程的.BIO 的 http 服务器,其中接收请求的部分,会将请求的第一行打印出来. 下面是浏览器发出的请求和 ...

  9. 说说C# 8.0 新增功能Index和Range的^0是什么?

    前言 在<C# 8.0 中使用 Index 和 Range>这篇中有人提出^0是什么意思?处于好奇就去试了,结果抛出异常.查看官方文档说^0索引与 sequence[sequence.Le ...

  10. PC端微信多开方式.bat(Windows批处理文件)

    start 微信安装路径\WeChat.exe start 微信安装路径\WeChat.exe