DMOPC '20 Contest 6 P4 - Land of the Carrot Trees II

View as PDF

Submit solution


Points: 17 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

A hungry rabbit is wandering on a tree with N nodes, numbered 1,2,,N when they notice that the i-th node has a carrot with flavour Fi. The rabbit then decides to ask himself Q times: "If the tree were rooted at node uj, how many subtrees would have at least kj distinct flavours of carrots?" Having overheard this mumbling, you decide to write a program to answer his queries.

Constraints

1N,Q5×105

1Fi,uj,kjN

The edges form a tree.

Subtask 1 [20%]

1N,Q2×103

Subtask 2 [50%]

kx=ky for all pairs (x,y).

Subtask 3 [30%]

No additional constraints.

Input Specification

The first line contains 2 space-separated integers, N and Q.

The second line of input contains N space-separated integers, F1,F2,,FN.

The next N1 lines contain 2 space-separated integers each, ai, bi, indicating there is an edge between ai and bi.

The next Q lines contain 2 space-separated integers each, uj, kj.

Output Specification

Output Q lines, where the j-th line contains the answer to the j-th query.

Sample Input

Copy
8 4
2 1 2 1 1 3 3 2
1 2
6 3
6 7
4 2
2 5
3 1
8 6
1 2
3 1
6 4
5 2

Sample Output

Copy
3
8
0
5

Explanation

The trees above are labeled with colours representing the flavours of the carrots.

For the first query, the tree is rooted at node 1, and we see that the subtrees rooted at nodes 1,3,6 have at least 2 distinct flavours of carrots.

For the second query, all subtrees have at least 1 distinct flavour of carrots.

For the third query, none of the subtrees have at least 4 distinct flavours of carrots.

For the fourth query, the tree is rooted at node 5, and we see that the subtrees rooted at nodes 1,2,3,5,6 have at least 2 distinct flavours of carrots.


Comments

There are no comments at the moment.