Tree
Time limit2sMemory limit512 MB
Given a rooted tree, process a mix of edge deletions and connectivity queries in order, answering YES or NO for each query.
- Level
Medium6 of 10
- Topics
- Union-find, Tree, DFS, Implementation
- Solved
- No attempts yet
Problem
A tree T is built from vertices and edges. A vertex is drawn as a circle and an edge is a line joining two vertices. The vertex at the very top is the root, and there is only one root. The N vertices are numbered 1 to N, and the root is always vertex 1.
A path joining vertices v and w is an ordered list of vertices with and , where and are joined by an edge. In a tree exactly one path exists between any two vertices. In Figure 1 the only path between vertex 3 and vertex 11 is (3, 4, 1, 7, 11).

Figure 1
Let P be the unique path from vertex v to the root. The vertex joined to v by an edge that lies on P is the parent of v. In Figure 1 the parent of 4, 7 and 9 is 1, and the parent of 2 and 11 is 7.
Removing one edge can cut the path between vertex pairs other than its two endpoints. In Figure 1, once the edge between 7 and 11 is removed, no path joins 8 and 5.
You are given the tree, and then edge removals and queries mixed in an arbitrary order. Carry out the operations in the given order and print the answer to each query.
Input
The first line contains the number of vertices N and the number of queries Q ().
The i-th of the next N-1 lines contains the parent a of vertex i+1 ().
The next (N-1)+Q lines contain the operations. N-1 of them have form (1) and Q of them have form (2).
(1) Two integers x and b (, ). This removes the edge joining b and the parent of b. The value b differs on every such line.
(2) Three integers x, c and d (, ). This asks whether a path joining c and d exists.
Output
Print the answers in the given order, one query per line. Print YES if a path exists and NO otherwise.