#LCA0000067. Ada và cây cam (Ada and Orange Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Ada và cây cam (Ada and Orange Tree)
Ada and Orange Tree
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
Ada has an orange tree of vertices numbered through . Each vertex has a shade in . The tree is rooted at . For each query , follow the unique path from to and count the distinct shades occurring in the union of the rooted subtrees of all vertices along that path. This is not merely a distinct-color count on the path itself.
Input
First line: . Next line has shades . Then edges and queries .
Output
Print the number of distinct shades for each query.
Subtasks
General constraints: ; ; ; dữ liệu là cây.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
10 7 1
1 2 1 4 5 6 6 8 9 9
0 9
9 3
3 4
4 6
4 5
4 8
1 3
1 2
2 7
4 4
8 6
0 6
7 0
7 2
0 0
2 3
Output
3
3
5
7
2
1
7
Explanation
For query , the path is one vertex and the union is the subtree rooted at , with three distinct shades.