-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13306.cpp
More file actions
90 lines (72 loc) · 1.6 KB
/
13306.cpp
File metadata and controls
90 lines (72 loc) · 1.6 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define MAXN 200050
int N,Q,parent[MAXN];
struct edge {
int src,dst;
};
struct query{
int flag,x,y;
};
vector<query> q;
vector<string> ans;
int find(int x){
if(parent[x] == x) return x;
return parent[x] = find(parent[x]);
}
void merge(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
parent[x] = y;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin >> N >> Q;
for(int i=0 ; i<=N ; i++) parent[i] = i;
int tmp[N+1];
for(int i=2 ; i<=N ; i++){
cin >> tmp[i];
}
for(int i=0 ; i<N-1+Q ; i++){
query qry;
cin >> qry.flag;
if(qry.flag){
cin >> qry.x >> qry.y;
q.push_back(qry);
} else {
cin >> qry.x;
q.push_back(qry);
}
}
for(int i=q.size()-1 ; i>=0 ; i--){
if(q[i].flag){
if(find(q[i].x)==find(q[i].y)) ans.push_back("YES\n");
else ans.push_back("NO\n");
}else {
merge(q[i].x, tmp[q[i].x]);
}
}
for(int i=ans.size()-1 ; i>=0 ; i--){
cout << ans[i];
}
}
// for(int i=0 ; i<N-1+Q ; i++){
// int flag;
// cin >> flag;
// if(flag){
// int a,b;
// cin >> a >> b;
// if(find(a) == find(b)){
// cout << "YES\n";
// }else cout << "NO\n";
// }else {
// int e;
// cin >> e;
// parent[e] = e;
// }
// }