Tree and Queries CodeForces - 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 vertices x, 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 ≤ nai ≠ 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.

Examples

Input
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
Output
2
2
1
0
1
Input
4 1
1 2 3 4
1 2
2 3
3 4
1 1
Output
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.

先把代码抄一遍再说:)

记录颜色那个很懵。。。

cnt[i]:记录 i 这种颜色出现的次数

cnk[i]:记录颜色出现 i 次的个数

 1 #pragma warning(disable:4996)
 2 #include<vector>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 
 9 const int maxn = 100005;
10 
11 int n, m, unit, DFS_clock;
12 int Start[maxn], End[maxn], arr[maxn], color[maxn], cnt[maxn], cnk[maxn], ans[maxn];
13 
14 vector<int> Tree[maxn];
15 
16 struct node {
17     int l, r, k;
18     int id;
19 }q[maxn];
20 
21 
22 bool cmp(const node& a, const node& b) {
23     if (a.l / unit == b.l / unit) return a.r < b.r;
24     return a.l < b.l;
25 }
26 
27 void DFS(int u, int pa) {
28     Start[u] = ++DFS_clock;
29     arr[DFS_clock] = color[u];
30     for (int i = 0; i < Tree[u].size(); i++) if (Tree[u][i] != pa) DFS(Tree[u][i], u);
31     End[u] = DFS_clock;
32 }
33 
34 void add(int pos) {
35     cnt[arr[pos]]++;
36     cnk[cnt[arr[pos]]]++;
37 }
38 
39 void remove(int pos) {
40     cnk[cnt[arr[pos]]]--;
41     cnt[arr[pos]]--;
42 }
43 
44 void Inite() {
45     DFS_clock = 0;
46     unit = sqrt(n);
47     memset(cnt, 0, sizeof(cnt));
48     memset(cnk, 0, sizeof(cnk));
49     for (int i = 0; i <= n; i++) Tree[i].clear();
50 }
51 
52 int main()
53 {
54     while (scanf("%d%d", &n, &m) != EOF) {
55         Inite();
56         for (int i = 1; i <= n; i++) scanf("%d", &color[i]);
57         for (int i = 2; i <= n; i++) {
58             int u, v;
59             scanf("%d%d", &u, &v);
60             Tree[u].push_back(v);
61             Tree[v].push_back(u);
62         }
63         DFS(1, -1);
64         for (int i = 1; i <= m; i++) {
65             int v, k;
66             scanf("%d%d", &v, &k);
67             q[i].l = Start[v];
68             q[i].r = End[v];
69             q[i].k = k;
70             q[i].id = i;
71         }
72         sort(q + 1, q + m + 1, cmp);
73         int L = q[1].l, R = L - 1;
74         for (int i = 1; i <= m; i++) {
75             while (L > q[i].l) add(--L);
76             while (L < q[i].l) remove(L++);
77             while (R < q[i].r) add(++R);
78             while (R > q[i].r) remove(R--);
79             ans[q[i].id] = cnk[q[i].k];
80         }
81         for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
82     }
83     return 0;
84 }

 

posted @ 2018-05-03 15:58  天之道,利而不害  阅读(441)  评论(0编辑  收藏  举报