题意翻译

给出一棵 n 个结点的树,每个结点有一个颜色 c i 。 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种。树的根节点是1。

感谢@elijahqi 提供的翻译

题目描述

You have a rooted tree consisting of n n n vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to n n n . Then we represent the color of vertex v v v as cv c_{v} cv​ . The tree root is a vertex with number 1.

In this problem you need to answer to m m m queries. Each query is described by two integers vj,kj v_{j},k_{j} vj​,kj​ . The answer to query vj,kj v_{j},k_{j} vj​,kj​ is the number of such colors of vertices x x x , that the subtree of vertex vj v_{j} vj​ contains at least kj k_{j} kj​ vertices of color x x x .

You can find the definition of a rooted tree by the following link: http://en.wikipedia.org/wiki/Tree\_(graph\_theory).

输入输出格式

输入格式:

The first line contains two integers n n n and m m m $ (2<=n<=10^{5}; 1<=m<=10^{5}) $ . The next line contains a sequence of integers c1,c2,...,cn c_{1},c_{2},...,c_{n} c1​,c2​,...,cn​ (1<=ci<=105) (1<=c_{i}<=10^{5}) (1<=ci​<=105) . The next n−1 n-1 n−1 lines contain the edges of the tree. The i i i -th line contains the numbers ai,bi a_{i},b_{i} ai​,bi​ $ (1<=a_{i},b_{i}<=n; a_{i}≠b_{i}) $ — the vertices connected by an edge of the tree.

Next m m m lines contain the queries. The j j j -th line contains two integers vj,kj v_{j},k_{j} vj​,kj​ $ (1<=v_{j}<=n; 1<=k_{j}<=10^{5}) $ .

输出格式:

Print m m m integers — the answers to the queries in the order the queries appear in the input.

输入输出样例

输入样例#1:

8 5
1 2 2 3 3 2 3 3
1 2
1 5
2 3
2 4
5 6
5 7
5 8
1 2
1 3
1 4
2 3
5 3
输出样例#1:

2
2
1
0
1
输入样例#2:

4 1
1 2 3 4
1 2
2 3
3 4
1 1
输出样例#2:

4

说明

A subtree of vertex v v v in a rooted tree with root r r r is a set of vertices $ {u :dist(r,v)+dist(v,u)=dist(r,u)} $ . Where dist(x,y) dist(x,y) dist(x,y) is the length (in edges) of the shortest path between vertices x x x and y y y .

Solution:

  本题树上莫队。

  求子树颜色个数,可以直接弄出dfs序,统计每个子树的入栈时间$inc$和$ouc$,然后对于询问变为dfs序上的区间颜色数查询,直接莫队就好了。

代码:

/*Code by 520 -- 10.19*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,m,to[N],net[N],h[N],cnt,a[N],bl[N],c[N];
int rc,dfn[N],inc[N],ouc[N],sum[N],ans[N];
struct node{
int l,r,k,id;
bool operator < (const node &a) const {return bl[l]==bl[a.l]?r<a.r:l<a.l;}
}t[N]; int gi(){
int a=;char x=getchar();
while(x<''||x>'') x=getchar();
while(x>=''&&x<='') a=(a<<)+(a<<)+(x^),x=getchar();
return a;
} il void Add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;} void dfs(int u,int lst){
dfn[++rc]=u,inc[u]=rc;
for(RE int i=h[u];i;i=net[i])
if(to[i]!=lst) dfs(to[i],u);
ouc[u]=rc;
} il void add(int x){sum[++c[a[x]]]++;} il void del(int x){sum[c[a[x]]--]--;} int main(){
n=gi(),m=gi(); int blo=sqrt(n),u,v;
For(i,,n) a[i]=gi(),bl[i]=(i-)/blo+;
For(i,,n) u=gi(),v=gi(),Add(u,v),Add(v,u);
dfs(,);
For(i,,m) u=gi(),v=gi(),t[i]=node{inc[u],ouc[u],v,i};
sort(t+,t+m+);
for(RE int i=,l=,r=;i<=m;i++){
while(l<t[i].l) del(dfn[l]),l++;
while(l>t[i].l) --l,add(dfn[l]);
while(r<t[i].r) ++r,add(dfn[r]);
while(r>t[i].r) del(dfn[r]),r--;
ans[t[i].id]=sum[t[i].k];
}
For(i,,m) printf("%d\n",ans[i]);
return ;
}
 
 
 
 
 
 

CF375D Tree and Queries的更多相关文章

  1. CF375D Tree and Queries(dsu on tree)

    思路 dsu on tree的板子,可惜人傻把 for(int i=fir[u];i;i=nxt[i]) 打成 for(int i=fir[u];i<=n;i++) 调了两个小时 这题要求维护& ...

  2. 「CF375D Tree and Queries」

    题目 \(dsu\ on\ tree\)的板子题了 \(dsu\ on\ tree\)本质上一种优秀通过轻重链剖分优化到\(O(nlogn)\)的暴力 一般用来解决没有修改的允许离线的子树查询问题 首 ...

  3. CF375D Tree and Queries 题解

    感觉CF的题目名都好朴素的样子 你谷链接 首先这题显然是个dsu on tree 但是我不会. 其次这题显然是个莫队.这我会啊! 然后会发现好像不是很对劲.因为每次询问都有一个k,貌似和传统的莫队数颜 ...

  4. 【题解】 Luogu CF375D Tree and Queries

    原题传送门 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 我博客中对莫队的详细介绍 莫队好题 我一上来想写线段树,随后觉得不好写并弃坑 我们可以看见没有修改操作,钦定莫队 但这是在树上,所以不能 ...

  5. cf375D. Tree and Queries(莫队)

    题意 题目链接 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. Sol 想到了主席树和启发式 ...

  6. 【题解】CF375D Tree and Queries

    Link \(\text{Solution:}\) 讲实话这题有点烦,不知道为啥改了下\(\text{dfs}\)就过了--原版本\(dfs\)好像没啥错啊-- 其实对于子树问题,我们求出原来树的\( ...

  7. [Codeforces Round #221 (Div. 1)][D. Tree and Queries]

    题目链接:375D - Tree and Queries 题目大意:给你一个有n个点的树,每个点都有其对应的颜色,给出m次询问(v,k),问v的子树中有多少种颜色至少出现k次 题解:先对所有的询问进行 ...

  8. Codeforces 375D Tree and Queries(DFS序+莫队+树状数组)

    题目链接  Tree and Queries 题目大意  给出一棵树和每个节点的颜色.每次询问$vj, kj$ 你需要回答在以$vj$为根的子树中满足条件的的颜色数目, 条件:具有该颜色的节点数量至少 ...

  9. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

随机推荐

  1. 贪心算法——字典序最小问题,Saruman‘s Army

    题目描述 Best Cow Line (POJ 3617) 给定长度为N的字符串S,要构造一个长度为N字符串T.T是一个空串,反复执行下列任意操作: 从S的头部删除一个字符,加到T的尾部: 从S的尾部 ...

  2. A Theoretical Analysis of Feature Pooling in Visual Recognition

    这篇是10年ICML的论文,但是它是从原理上来分析池化的原因,因为池化的好坏的确会影响到结果,比如有除了最大池化和均值池化,还有随机池化等等,在eccv14中海油在顶层加个空间金字塔池化的方法.可谓多 ...

  3. mysql show profiles 使用分析sql 性能

    Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看一下我的数据库版本 MySQL> Select  version(); +-------- ...

  4. [SDOI2009]HH的项链 BZOJ1878

    分析: 听说是莫队裸题,很显然,我并不喜欢莫队. 我们可以考虑将询问离线,以右端点排序,之后从1枚举到n,依次树状数组中修改i和last[i],之后当i==询问的右节点时,find一下答案就可以了. ...

  5. CoreFoundation对象的内存管理

    近来没什么新项目做,想学习一些swift开源项目,看了几个文件感觉有点懵,可能水平还没达到,等用到具体内容的时候再去仔细看吧. 关于现在的项目,想想单元测试还可以完善一下,就在framwork工程中写 ...

  6. idea java方法中 传多个参数对象 的复制粘贴快速处理方法

    比如像这种的传多个参数对象,我是直接复制过来,然后把第一个字母改成大写,然后后面的实例对象敲一个第一个字符的小写,回车就直接出来了 在写调用参数的地方,ctrl+p 调出提示,然后按下提示里的实例的第 ...

  7. Please restart this script from an administrative PowerShell

    问题 在上一篇<MSBUILD : error MSB3428: 未能加载 Visual C++ 组件"VCBuild.exe">,安装时失败了,提示: 解决办法 1. ...

  8. C#_正则表达式

    概述 正则表达式,主要是用符号描述了一类特定的文本(模式).而正则表达式引擎则负责在给定的字符串中,查找到这一特定的文本. 本文主要是列出常用的正则表达式符号,加以归类说明.本文仅仅是快速理解了正则表 ...

  9. 01_python2.x和python3.x中range()的区别

    Py2.x 1) .range 和xrange都是经常使用的,特别是range()返回一个列表 2) .xrange()一般用来创建迭代对象 Py3.x xrange()不存在了,只有range()而 ...

  10. Intellij Idea 创建JavaWeb项目入门(一)

    Idea创建JavaWeb项目步骤:1.打开Intellij Idea IDE,然后点击Create New Project 2.左侧选择Java Enterprise,右侧选择Web Applica ...