CoderForces-375D
You have a rooted tree consisting of 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. Then we represent the color of vertex v as cv. The tree root is a vertex with number 1.
In this problem you need to answer to m queries. Each query is described by two integers vj, kj. The answer to query vj, kj is the number of such colors of verticesx, that the subtree of vertex vj contains at least kj vertices of color x.
You can find the definition of a rooted tree by the following link:http://en.wikipedia.org/wiki/Tree_(graph_theory).
Input
The first line contains two integers n and m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105). The next line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105). The next n - 1 lines contain the edges of the tree. The i-th line contains the numbers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — the vertices connected by an edge of the tree.
Next m lines contain the queries. The j-th line contains two integers vj, kj (1 ≤ vj ≤ n; 1 ≤ kj ≤ 105).
Output
Print m integers — the answers to the queries in the order the queries appear in the input.
Example
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
2
2
1
0
1
4 1
1 2 3 4
1 2
2 3
3 4
1 1
4
Note
A subtree of vertex v in a rooted tree with root r is a set of vertices{u : dist(r, v) + dist(v, u) = dist(r, u)}. Where dist(x, y) is the length (in edges) of the shortest path between vertices x and y.
题解:首先,通过dfs,可以把查询一棵树的子树转化为查询一段区间[l,r]。接下来,把整个区间分成n/sqrt(n)+1份,将查询按照l所在的区间排序,在同一区间内,将查询按r排序(由大到小)。对于查询,用一个数组表示某颜色数为x的颜色的个数,查询这个数组用分块法查询。
AC代码:
#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<vector>
using namespace std; typedef long long ll;
const int maxn = + ;
const int SIZE = ;
int bnt[maxn], block[maxn / SIZE + ], cnt[maxn];
int l[maxn], r[maxn], dfs_clock;
int c[maxn], cval[maxn], ans[maxn], n, m;
vector<int> g[maxn]; struct Query
{
int l, r, k, id;
Query() {}
Query(int lx, int rx, int kk, int i)
{
l = lx;
r = rx;
k = kk;
id = i;
}
bool operator <(const Query &q) const
{
if (l / SIZE != q.l / SIZE)
return l<q.l;
return r>q.r;
}
}querys[maxn]; void dfs(int u, int fa)
{
l[u] = ++dfs_clock;
cval[l[u]] = c[u];
int sz = g[u].size();
for (int i = ; i<sz; ++i)
{
int v = g[u][i];
if (v == fa)
continue;
dfs(v, u);
}
r[u] = dfs_clock;
} void Insert(int k, int x)
{
bnt[k] += x;
block[k / SIZE] += x;
}
void Add(int v)
{
int p = ++cnt[cval[v]];
Insert(p, );
Insert(p - , -);
} void Dec(int v)
{
int p = --cnt[cval[v]];
Insert(p, );
Insert(p + , -);
} int cal(int k)
{
int sum = , p = k / SIZE;
for (int i = k; i<(p + )*SIZE; ++i)
sum += bnt[i];
for (int i = p + ; i*SIZE<maxn; ++i)
sum += block[i];
return sum;
} void solve()
{
memset(cnt, , sizeof(cnt));
memset(bnt, , sizeof(bnt));
memset(block, , sizeof(block));
Insert(, n);
Add();
int l = , r = ;
for (int i = ; i<m; ++i)
{
while (querys[i].l<l) Add(--l);
while (querys[i].r>r) Add(++r);
while (querys[i].l>l) Dec(l++);
while (querys[i].r<r) Dec(r--);
ans[querys[i].id] = cal(querys[i].k);
}
} int main()
{
cin >> n >> m;
for (int i = ; i <= n; ++i)
g[i].clear();
dfs_clock = ;
for (int i = ; i <= n; ++i)
cin >> c[i];
int u, v, k;
for (int i = ; i<n - ; ++i)
{
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
} dfs(, -); for (int i = ; i<m; ++i)
{
cin >> v >> k;
querys[i] = Query(l[v], r[v], k, i);
}
sort(querys, querys + m);
solve();
for (int i = ; i < m; ++i)
cout << ans[i] << endl;
return ;
}
CoderForces-375D的更多相关文章
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- coderforces 731c
题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...
- coderforces 721b
题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 375D:Tree and Queries
Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...
- CoderForces 280B(记忆化搜索)
题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...
- codeforces 375D . Tree and Queries 启发式合并 || dfs序+莫队
题目链接 一个n个节点的树, 每一个节点有一个颜色, 1是根节点. m个询问, 每个询问给出u, k. 输出u的子树中出现次数大于等于k的颜色的数量. 启发式合并, 先将输入读进来, 然后dfs完一个 ...
- CF 375D. Tree and Queries【莫队 | dsu on tree】
题意: 一棵树,询问一个子树内出现次数$≥k$的颜色有几种 强制在线见上一道 用莫队不知道比分块高到哪里去了,超好写不用调7倍速度!!! 可以用分块维护出现次数这个权值,实现$O(1)-O(\sqrt ...
- CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】
传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...
- CoderForces 689A Mike and Cellphone (水题)
题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现. 析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8, ...
随机推荐
- C语言:互质
今天遇到一道奇怪的程序题,和平常的不同.同样都是互质,但是一般的题目都是判断两个数字是否互质,但这道题则是给定一个数字n,要求输出所有小于等于n的与n互质的数,题目已经在下面给出: 质数与互质概念不是 ...
- 深入理解计算机系统 第二章 信息的表示和处理 Part2 第二遍
<深入理解计算机系统> 第三版 第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了 29 ~ 34 页) 第一遍对应笔记链接 https://www.cnblogs.com/s ...
- Centos 6/7 常用命令总结 (基础)
Centos 6/7 常用命令总结 (基础): 参考链接:https://www.cnblogs.com/linhaifeng/p/6045600.html 目录介绍: a) bin目录:用来存放常用 ...
- C++中对C的扩展学习新增内容———面向对象(继承)函数扩展性及虚函数机制
1.c语言中的多态,动态绑定和静态绑定 void do_speak(void(*speak)()) { speak(); } void pig_speak() { cout << &quo ...
- 【dp】you are the one
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283 题解: 当最优解下, a1在j的位置排出, 则a2 ——aj-1 和 aj——an为两个独立事件 ...
- centos7清理矿机木马qw3xT,kpgrbcc
腾讯云报告了root口令被暴力破解,并种了木马kpgrbcc 昨晚找到/usr/bin/ rm -rf kpgrbcc 删除 rm -rf kpgrbcb 删除 并ps -ef | grep kpg ...
- Java基础知识总结之垃圾回收机制
垃圾回收机制 Java垃圾回收机制是Java语言的重要功能之一.当程序创建对象,数组等引用类型对象时,系统会自动在内存区为之分配一块内存,对象就保存在这块内存区内,当这块内存不再被任何变量引用时,这块 ...
- Selenium网页自动登录项目(基于Python从0到1)
Selenium是一个自动化测试工具,利用它我们可以驱动浏览器执行特定的动作,如点击.下拉等操作. 本文讲述的是通过自动化的方式登陆某一网站,其中包含Selenium+python自动化项目环境如何部 ...
- 【Luogu P2471】[SCOI2007]降雨量
Luogu P2471 啊啊啊啊这真是一道史上最毒瘤的题目!!!!! 题意就是给出n个年份的降雨量 询问:"自从\(y\)年以来\(x\)年的降雨量最大"的正确性. 显然有多种情况 ...
- Flow入门初识
Flow是facebook出品的JavaScript静态类型检查工具. 由于JavaScript是动态类型语言,它的灵活性也会造成一些代码隐患,使用Flow可以在编译期尽早发现由类型错误引起的bug, ...