-
Notifications
You must be signed in to change notification settings - Fork 1
/
sol2.cpp
48 lines (42 loc) · 965 Bytes
/
sol2.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
// Written by: wyattwismer
#include <bits/stdc++.h>
using namespace std;
#define BIG 100005
#define WIDTH 20
int par[BIG*2][WIDTH];
void insert(int x, int p){
par[x][0] = p;
for(int i=1;i<WIDTH;i++)
par[x][i] = par[par[x][i-1]][i-1];
}
int kth(int x, int k){
for(int i=0;k;i++,k>>=1) if(k&1)
x = par[x][i];
return x;
}
int main(){
int T, n, Q, t, x, y, k;
cin>>T;
while(T--){
for(int i=0;i<2*BIG;i++) for(int j=0;j<WIDTH;j++) par[i][j] = 0;
cin>>n;
for(int i=0;i<n;i++){
cin>>x>>y;
insert(x,y);
}
cin>>Q;
while(Q--){
cin>>t;
if(t==0){
cin>>y>>x;
insert(x,y);
} else if (t==1){
cin>>x;
for(int j=0;j<WIDTH;j++) par[x][j]=0;
}else{
cin>>x>>k;
cout << kth(x,k) << endl;
}
}
}
}