-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathD.cpp
107 lines (105 loc) · 1.82 KB
/
D.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// LUOGU_RID: 158570685
#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
using i64=long long ;
using ld=long double;
const int N=2e5+10;
const int mod=1e9+7;
int f[N][22];
int w[N][22];
vector<int> a[N];
int d[N];
int pre[N];
int find(int x){
if(x==pre[x]) return x;
return pre[x]=find(pre[x]);
}
void add(int x,int y){
x=find(x);
y=find(y);
pre[y]=x;
}
void dfs(int x){
for(auto y:a[x]){
d[y]=d[x]+1;
f[y][0]=x;
add(x,y);
for(int j=1;j<20;j+=1){
f[y][j]=f[f[y][j-1]][j-1];
w[y][j]=w[y][j-1]|w[f[y][j-1]][j-1];
}
dfs(y);
}
}
int lca(int x,int y){
int o,p;
o=p=0;
// cout<<x<<" "<<y<<endl;
for(int j=19;j>=0;j--){
if(d[x]>d[y]){
if(d[f[x][j]]>=d[y]) o|=w[x][j],x=f[x][j];
}else if(d[y]>d[x]){
if(d[f[y][j]]>=d[x]) p|=w[y][j],y=f[y][j];
}
}
// cout<<x<<" "<<y<<" "<<p<<endl;
if(x==y){
if(p==0||p==3) return -1;
return p;
}
for(int j=19;j>=0;j--){
if(f[x][j]!=f[y][j]){
o|=w[x][j];
p|=w[y][j];
x=f[x][j],y=f[y][j];
}
}
o|=w[x][0],p|=w[y][0];
x=f[x][0];
y=f[y][0];
if(o==1&&p==2) return 2;
return -1;
}
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++) pre[i]=i;
for(int i=1;i<=n;i++){
cin>>f[i][0]>>w[i][0];
if(f[i][0]==-1) f[i][0]=w[i][0]=0;
else w[i][0]=1<<w[i][0];
if(f[i][0]) a[f[i][0]].push_back(i);
}
for(int i=1;i<=n;i++){
if(f[i][0]==0){
d[i]=1;
dfs(i);
}
}
int q;
cin>>q;
while(q--){
int t,x,y;
cin>>t>>x>>y;
if(find(x)!=find(y)){
cout<<"NO"<<endl;
continue;
}
if(lca(x,y)==t){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}