#P11108. [ROI 2023] 蜗牛与富士山 (Day 2)
[ROI 2023] 蜗牛与富士山 (Day 2)
Background
Translated from ROI 2023 D2T1.
After climbing to the top of Mount Fuji, the snail wants to go down. On the slope there is a trail system that looks like a binary tree.
The tree contains nodes connected by trails. The root of the tree is at the summit. For some nodes, the trails end there; these nodes are the leaves of the tree. Every non-leaf node has two trails going downhill: one to the left and one to the right.
The snail wants to start at the root and go down along the tree to reach one of the leaves. Along the way, at each node it can choose one of the two directions to continue descending: left or right.
At the beginning, at the summit, the snail may start descending in either direction (left or right). At each subsequent node, if it chooses a direction different from the direction chosen at the previous node, then it makes a turn.
Since turning is inconvenient for the snail, on the whole path from the root to a leaf, the snail is willing to make at most turns.
Problem Description
The nodes of the tree are numbered from to , and the root has number . You are given queries. Each query gives a node . You need to find the number of leaf nodes that the snail can reach on a path that starts from the root, passes through node , and makes at most turns while descending.
Input Format
The first line contains three integers , representing the number of nodes in the tree, the maximum number of turns the snail is willing to make, and the number of queries.
The next lines describe the structure of the tree. On line , the first integer denotes the number of trails (children) of node ( or ). If , then the same line contains two integers and , denoting the node numbers connected by the left and right trails starting from node ( ). It is guaranteed that the input satisfies the requirements.
The next lines contain the queries. Line contains one integer , meaning the node that the snail's path must pass through ( ).
Output Format
For each query, output one integer per line, the answer to that query.
7 1 4
2 2 4
0
2 6 5
2 3 7
0
0
0
1
4
3
5
3
2
1
0
Hint
The sample tree looks like this.

The first query (gray nodes are reachable leaves, dashed lines are unreachable nodes):

The second query:

The third query:

The fourth query (since at most one turn is allowed, there is no valid route, so the output is ):

For of the testdata, , , .
| Subtask | Score | Special Properties |
|---|---|---|
| , all are leaf nodes. | ||
| . | ||
| . | ||
| . | ||
| all are leaf nodes. | ||
| none. |
Translated by ChatGPT 5