#P6374. 「StOI-1」树上询问
「StOI-1」树上询问
Description
Given an unrooted tree with nodes, there are queries.
Each query provides a parameter triple . You need to find how many nodes satisfy that, when the tree is rooted at , the LCA of and is .
Input Format
The first line contains integers, and .
The next lines each contain integers, describing an edge of the tree.
The next lines each contain integers, which are .
Output Format
Output lines. Each line contains one integer: for each triple, the number of valid nodes .
10 5
1 2
1 3
2 4
2 5
2 10
5 6
3 7
7 8
7 9
4 6 2
4 10 1
6 8 3
9 10 2
4 10 5
7
0
1
4
0
5 3
1 3
1 5
3 4
3 2
5 2 3
5 2 1
2 4 5
2
1
0
20 10
1 2
1 3
1 4
2 5
2 6
3 10
4 13
4 14
6 7
6 8
10 11
4 15
4 16
8 9
11 12
16 17
16 18
16 19
17 20
15 19 16
1 12 1
20 20 20
7 7 8
1 8 3
5 20 2
2 9 6
9 12 1
9 12 2
9 12 3
4
16
20
0
0
5
2
10
2
1
Hint
Explanation for Sample 2

For the first query, the valid are and .
For the second query, the valid is .
Constraints
This problem is tested by subtasks:
: , .
: , , the tree degenerates into a chain.
: , , the testdata is not random.
: , .
For all testdata: , .
Note: the testdata is not very strong, so there is no need to micro-optimize or use fast input/output.
Translated by ChatGPT 5
京公网安备 11011102002149号