#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 nn nodes connected by n1n-1 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 kk turns.

Problem Description

The nodes of the tree are numbered from 11 to nn, and the root has number 11. You are given qq queries. Each query gives a node uiu_i. 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 uiu_i, and makes at most kk turns while descending.

Input Format

The first line contains three integers n,k,qn, k, q, 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 nn lines describe the structure of the tree. On line ii, the first integer tit_i denotes the number of trails (children) of node ii (ti=0t_i = 0 or 22). If ti=2t_i = 2, then the same line contains two integers lil_i and rir_i, denoting the node numbers connected by the left and right trails starting from node ii ( 1li,rin1 \le l_i, r_i \le n ). It is guaranteed that the input satisfies the requirements.

The next qq lines contain the queries. Line ii contains one integer uiu_i, meaning the node that the snail's path must pass through ( 1uin1 \le u_i \le n ).

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 00):

For 100%100\% of the testdata, 3n2000003 \le n \le 200000, 0kn0 \le k \le n, 1q2000001 \le q \le 200000.

Subtask Score Special Properties
11 1111 n500,q500n \le 500, q \le 500, all uiu_i are leaf nodes.
22 1212 n500,q500n \le 500, q \le 500.
33 1010 k=nk = n.
44 1414 k=0k = 0.
55 1919 all uiu_i are leaf nodes.
66 3434 none.

Translated by ChatGPT 5