PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000087. Cây và truy vấn (Tree and Queries)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cây và truy vấn (Tree and Queries)

Tree and Queries

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree rooted at vertex 11 with nn vertices numbered from 11 to nn. Vertex uu has color cuc_u. For each query (v,k)(v,k), count the colors appearing at least kk times in the subtree rooted at vv, including vv. Output 00 if no color qualifies. Queries do not modify the tree.

Input

  • Line 1: integers n,mn,m.
  • Line 2: nn integers c1,…,cnc_1,\ldots,c_n.
  • The next n−1n-1 lines each contain u,vu,v, describing an undirected edge; together the edges form a tree.
  • The last mm lines each contain a query v,kv,k.

Output

Print mm answers, one per line, in query order.

Subtasks

  • Subtask 1 (10 points): 1≤n≤351\le n\le 35, 1≤m≤451\le m\le 45.
  • Subtask 2 (20 points): 1≤n≤10001\le n\le 1000, 1≤m≤10001\le m\le 1000.
  • Subtask 3 (30 points): 1≤n≤200001\le n\le 20000, 1≤m≤200001\le m\le 20000.
  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000, 1≤m≤1000001\le m\le 100000; 1≤ci≤1000001\le c_i\le 100000.

General limits: 2≤n≤1000002\le n\le 100000, 1≤m≤1000001\le m\le 100000, 1≤ci≤1000001\le c_i\le 100000, 1≤v≤n1\le v\le n, 1≤k≤1000001\le k\le 100000. Vertices are numbered from 1 to nn.

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

Explanation

For query (1,2), colors 2 and 3 both occur at least twice. For (1,3), both still qualify; for (1,4), only color 3 qualifies.