Under construction

Still being built. Things may change

Tree

Time limit2sMemory limit512 MB

Summary
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 TT 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 NN vertices are numbered 1 through NN, and the root is always vertex 1.

A path joining two vertices vv and ww is a sequence of vertices (v0,v1,…,vm)(v_0, v_1, \dots, v_m) in which neighboring vertices viv_i and vi+1v_{i+1} are joined by an edge, v0=vv_0 = v, and vm=wv_m = w. 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 (3,4,1,7,11)(3, 4, 1, 7, 11).

Figure 1

Let PP be the unique path from vertex vv to the root. The vertex that is joined to vv by an edge and lies on PP is the parent of vv. 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 NN and the number of queries QQ (1≤N,Q≤200 0001 \le N, Q \le 200\,000).

The ii-th of the next N−1N-1 lines contains one integer aa, the parent of vertex i+1i+1 (1≤a≤N1 \le a \le N). The given parent relation always forms a tree rooted at vertex 1.

Each of the next QQ lines contains three integers bb, cc and dd (1≤b,c≤N1 \le b, c \le N, and dd is 0 or 1). If d=0d = 0, only handle the query asking whether a path joins bb and cc. If d=1d = 1, handle the same query first, then remove the edge joining bb and the parent of bb when the answer is YES, or the edge joining cc and the parent of cc 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 QQ queries in order, one per line. Print YES when a path exists and NO when it does not.

Examples2

  1. Example 1

    Input
    3 4
    1
    1
    2 3 1
    1 3 0
    2 3 1
    1 3 1
    
    Expected output
    YES
    YES
    NO
    NO
    
  2. Example 2

    Input
    11 7
    7
    4
    1
    9
    11
    1
    11
    1
    3
    7
    11 9 1
    8 5 0
    3 9 0
    6 3 1
    10 9 1
    3 10 1
    1 4 1
    
    Expected output
    YES
    NO
    YES
    NO
    NO
    YES
    YES