Äventyr 2
Time limit1sMemory limit256 MB
On a tree, timelines get marked over time, and after each mark you must report the distance from a queried vertex to the nearest marked vertex.
- Level
Hard8 of 10
- Topics
- Tree, BFS, Prefix sum, DFS
- Solved
- No attempts yet
Problem
"Äventyr bortom tidpunkten, Sista Fiolen."
You travel between timelines to find the legendary violin of a forgotten country. Each timeline is a vertex of a tree, and an edge of the tree means you can move between the two timelines it joins. The timelines are numbered from to . Some timelines hold the legendary violin and the rest do not. At the start, no timeline holds the violin. After the tree is given, process queries of the following two kinds.
1 u: timeline becomes a timeline that holds the violin.2 u: print the minimum number of edges a journey starting at timeline must cross to reach a timeline that holds the violin. If no timeline holds the violin, print .
Input
The first line contains and . ()
The second line contains integers . means that the parent of timeline in the tree is timeline . (, ) The given edges always form a single tree. When , the second line is empty.
Each of the next lines contains one query in the form c v. If it is a query of type 1, and if it is a query of type 2. () A query of type 1 is never given twice for the same timeline.
Output
For every query of type 2, print its result on its own line, in order.