#P4092. [HEOI2016/TJOI2016] 树
[HEOI2016/TJOI2016] 树
Description
In 2016, Jiayuan jiejie had just learned about trees and was very happy. Now she wants to solve the following problem: given a rooted tree with root , there are two types of operations:
-
Mark operation: put a mark on some node. (At the very beginning, only node is marked, and all other nodes are unmarked. You may mark the same node multiple times.)
-
Query operation: ask for the nearest marked ancestor of some node (the node itself also counts as its own ancestor).
Can you help her?
Input Format
The first line contains two positive integers and , denoting the number of nodes and the number of operations.
The next lines each contain two positive integers () indicating a directed edge from to . These edges form a rooted tree with root , and edges are directed from parent to child.
The next lines are of the form oper num. When oper is C, it denotes a mark operation on node num. When oper is Q, it denotes a query operation on node num.
Output Format
For each query operation, output one integer: the nearest marked ancestor of the given node (including the node itself). Print each answer on its own line.
5 5
1 2
1 3
2 4
2 5
Q 2
C 2
Q 2
Q 5
Q 3
1
2
2
1
Hint
- 30% of the testdata: .
- 70% of the testdata: .
- 100% of the testdata: .
Translated by ChatGPT 5
京公网安备 11011102002149号