#P8844. [传智杯 #4 初赛] 小卡与落叶
[传智杯 #4 初赛] 小卡与落叶
Background
Sitting on a fast-moving train and looking at the yellowing leaves outside the window, Xiao Ka thought, “Another winter.” This is a season when everything withers. When a cold wind blows, the leaves turn yellow; when another cold wind blows, the ground becomes covered in gold.
Out of boredom, Xiao Ka discovered that leaves turn yellow following a pattern: on every tree, only the lower half is yellow, and the upper part is all green. Xiao Ka wondered how to count the number of yellow leaves.
Problem Description
You are given a rooted tree with nodes. The root is node , and the depth of the root is . Initially, all nodes in the entire tree are green.
Xiao Ka has operations.
Operation 1: Recolor the whole tree to green, then make all nodes with depth turn yellow.
Operation 2: Query how many yellow nodes are in the subtree of a node .
Input Format
The first line contains two positive integers , denoting the number of nodes in the tree and the number of operations.
The next lines each contain two positive integers , denoting an edge in the tree.
The next lines each contain two positive integers . If , it means Operation 1; otherwise, it means Operation 2.
Output Format
For each Operation 2, output one line with one positive integer, denoting the number of yellow nodes in the subtree of .
5 9
1 2
1 3
2 4
4 5
1 3
2 5
2 2
2 1
1 2
2 1
2 4
2 5
2 2
1
2
2
4
2
1
3
Hint
The tree in Sample 1 is as follows:

In the first coloring, nodes and are colored yellow. Querying the subtrees of nodes , the answers are , respectively.
In the second coloring, nodes are colored yellow. Querying the subtrees of nodes , the answers are , respectively.
Translated by ChatGPT 5