Tree
Time limit2sMemory limit512 MB
After each query asks whether two vertices are still connected, the tree may lose one edge based on the answer, so connectivity must be tracked under online deletions.
- Level
Hard8 of 10
- Topics
- Tree, Union-find, DFS, Implementation
- Solved
- No attempts yet
Problem
Tree is made of vertices and edges. Each circle in Figure 1 is a vertex, and each line joining two vertices is an edge. The vertex drawn at the top is the root, and there is exactly one root. The vertices are numbered 1 through , and the root is always vertex 1.
A path joining two vertices and is a sequence of vertices in which neighboring vertices and are joined by an edge, , and . In a tree, exactly one such path exists between any two vertices. In Figure 1 the only path joining vertex 3 and vertex 11 is .

Figure 1
Let be the unique path from vertex to the root. The vertex that is joined to by an edge and lies on is the parent of . In Figure 1 the parent of 4, 7 and 9 is 1, and the parent of 2 and 11 is 7. The root has no parent.
When one edge is removed, pairs of vertices other than the two endpoints of that edge can also lose the path between them. Removing the edge between 7 and 11 in Figure 1 leaves no path between 8 and 5.
You are given the tree, and then queries and edge removals mixed in an arbitrary order. Carry out the operations in the given order and print the answer to every query.
Input
The first line contains the number of vertices and the number of queries ().
The -th of the next lines contains one integer , the parent of vertex (). The given parent relation always forms a tree rooted at vertex 1.
Each of the next lines contains three integers , and (, and is 0 or 1). If , only handle the query asking whether a path joins and . If , handle the same query first, then remove the edge joining and the parent of when the answer is YES, or the edge joining and the parent of when the answer is NO. If the edge to remove does not exist or was already removed, no edge is removed.
Output
Print the answers to the queries in order, one per line. Print YES when a path exists and NO when it does not.