#ABC477G. 树上的频率询问 / Frequency Query on Tree
树上的频率询问 / Frequency Query on Tree
Problem Statement
You are given a tree with vertices numbered to . The -th edge connects vertex and vertex . Also, integer is written on vertex .
Process queries. In each query, you are given integers ; find the number of integers satisfying the following condition.
- Let be the number of vertices with integer written on them among the vertices on the path connecting vertex and vertex . Then, holds.
Constraints
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Each query is given in the following format:
Output
Output lines. The -th line should contain the answer to the -th query.
5 4
1 2 3 1 2
1 2
2 3
3 4
4 5
1 5 1 2
1 4 2 3
3 5 1 1
2 4 2 5
3
1
3
0
Consider the first query. The vertices on the path connecting vertex and vertex are, in order, vertex , vertex , vertex , vertex , and vertex . The integers written on these vertices are, in order, . Thus, there are three integers satisfying the condition: .
11 8
1 2 3 1 4 2 2 5 2 1 2
1 2
1 3
2 4
1 5
4 6
6 7
3 8
7 9
4 10
9 11
8 11 3 5
6 10 1 5
8 9 5 6
1 11 1 4
4 6 2 5
5 6 2 6
3 7 1 1
5 7 5 8
1
2
0
1
0
2
1
0
- Source: AtCoder ABC 477 G
相关
在下列比赛中: