-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13306.cpp
More file actions
70 lines (57 loc) · 1.18 KB
/
Copy path13306.cpp
File metadata and controls
70 lines (57 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<cstdio>
#include<vector>
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int par[201010];
int parentStr[201010];
struct Query{
int x, arg1, arg2;
};
stack<Query> query;
stack<string> ans;
int find(int u){
if(u == par[u]) return u;
return par[u] = find(par[u]);
}
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
int N, Q, a, x, bc, d;
cin >> N >> Q;
for(int i=1; i<=N; i++){
par[i] = i;
}
for(int i=1; i<=N-1; i++){
cin >> a;
parentStr[i+1] = a;
}
for(int i=0; i<(N-1+Q); i++){
cin >> x >> bc;
if(x == 0){
query.push({x, bc, -1});
}else{
cin >> d;
query.push({x, bc, d});
}
}
while(!query.empty()){
Query q = query.top();
if(q.x == 0){
par[q.arg1] = parentStr[q.arg1];
}else{
if(find(q.arg1) == find(q.arg2)){
ans.push("YES");
}else{
ans.push("NO");
}
}
query.pop();
}
while(!ans.empty()){
cout << ans.top() << '\n';
ans.pop();
}
}