#P6374. 「StOI-1」树上询问

「StOI-1」树上询问

Description

Given an unrooted tree with nn nodes, there are qq queries.

Each query provides a parameter triple (a,b,c)(a,b,c). You need to find how many nodes ii satisfy that, when the tree is rooted at ii, the LCA of aa and bb is cc.

Input Format

The first line contains 22 integers, nn and qq.

The next n1n-1 lines each contain 22 integers, describing an edge of the tree.

The next qq lines each contain 33 integers, which are (a,b,c)(a,b,c).

Output Format

Output qq lines. Each line contains one integer: for each triple, the number of valid nodes ii.

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 ii are 33 and 44.

For the second query, the valid ii is 11.


Constraints

This problem is tested by subtasks:

subtask1(20pts)subtask1 (20pts): 1n10001 \leq n \leq 1000, 1q5001 \leq q \leq 500.

subtask2(15pts)subtask2 (15pts): 1n1051 \leq n \leq 10^{5}, 1q1051 \leq q \leq 10^{5}, the tree degenerates into a chain.

subtask3(25pts)subtask3 (25pts): 1n5×1051 \leq n \leq 5 \times 10^{5}, 1q1051 \leq q \leq 10^{5}, the testdata is not random.

subtask4(40pts)subtask4 (40pts): 1n5×1051 \leq n \leq 5 \times 10^{5}, 1q2×1051 \leq q \leq 2 \times 10^{5}.

For all testdata: 1n5×1051 \leq n \leq 5 \times 10^{5}, 1q2×1051 \leq q \leq 2 \times 10^{5}.

Note: the testdata is not very strong, so there is no need to micro-optimize or use fast input/output.

Translated by ChatGPT 5