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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 with vertices numbered from to . Vertex has color . For each query , count the colors appearing at least times in the subtree rooted at , including . Output if no color qualifies. Queries do not modify the tree.
Input
- Line 1: integers .
- Line 2: integers .
- The next lines each contain , describing an undirected edge; together the edges form a tree.
- The last lines each contain a query .
Output
Print answers, one per line, in query order.
Subtasks
- Subtask 1 (10 points): , .
- Subtask 2 (20 points): , .
- Subtask 3 (30 points): , .
- Subtask 4 (40 points): , ; .
General limits: , , , , . Vertices are numbered from 1 to .
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.