#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int B = 21;
const int maxn=2e5+5;
int n,k;
int p[maxn];
int L[maxn];
int par[B][maxn];
pair<int,int> sp[2][B][maxn];
vector<pair<int,int>>E[maxn];
int ata(int nd){
if(p[nd] == nd) return nd;
return p[nd] = ata(p[nd]);
}
void dfs(int nd,int pr){
par[0][nd] = pr;
L[nd] = L[pr] + 1;
for(auto i : E[nd]){
if(i.first != pr){
sp[0][0][i.first] = {1,i.second};
sp[1][0][i.first] = {1,i.second};
dfs(i.first,nd);
}
}
}
bool LCA(int a,int b){
bool tr = 1;
int cep = 1e9;
int sag = 0;
if(L[a] > L[b]){
for(int i = B-1;i >= 0;i--){
if(par[i][a] and L[a] - (1<<i) >= L[b]){
tr &= sp[0][i][a].first;
tr &= (cep > sp[0][0][a].second);
cep = sp[0][i][a].second;
a = par[i][a];
if(!tr) return 0;
}
}
}
else if(L[b] > L[a]){
for(int i = B-1;i >= 0;i--){
if(par[i][b] and L[b] - (1<<i) >= L[a]){
tr &= sp[1][i][b].first;
tr &= (sag < sp[1][0][b].second);
sag = sp[1][i][b].second;
b = par[i][b];
if(!tr) return 0;
}
}
}
if(a == b) return tr;
for(int i = B-1;i >= 0;i--){
if(par[i][a] and par[i][b] and par[i][a] != par[i][b]){
tr &= sp[0][i][a].first;
tr &= sp[1][i][b].first;
tr &= (cep > sp[0][0][a].second);
tr &= (sag < sp[1][0][b].second);
if(!tr) return 0;
// assert(sp[0][0][a].second >= sp[0][i][a].second);
// assert(sp[1][0][b].second <= sp[1][i][b].second);
cep = sp[0][i][a].second;
sag = sp[1][i][b].second;
a = par[i][a];
b = par[i][b];
}
}
tr &= (cep > sp[0][0][a].second);
tr &= (sag < sp[1][0][b].second);
tr &= (sp[0][0][a].second > sp[1][0][b].second);
return tr;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for(int i = 1;i <= n;i++) p[i] = i;
vector<tuple<int,int,int>>quer;
for(int i = 1;i < n + k;i++){
char tp; cin >> tp;
if(tp == 'S'){
int x,y; cin >> x >> y;
E[x].push_back({y,i});
E[y].push_back({x,i});
quer.push_back({0,x,y});
}
else if(tp == 'Q'){
int x,y; cin >> x >> y;
quer.push_back({1,x,y});
}
else {
int x; cin >> x;
quer.push_back({2,x,-1});
}
}
dfs(1,0);
for(int i = 1;i < B;i++){
for(int j = 1;j <= n;j++){
par[i][j] = par[i-1][par[i-1][j]];
if(!par[i][j]) continue;
if(sp[0][i-1][j].first and sp[0][i-1][par[i-1][j]].first and sp[0][i-1][j].second > sp[0][0][par[i-1][j]].second){
sp[0][i][j] = {1,sp[0][i-1][par[i-1][j]].second};
}
if(sp[1][i-1][j].first and sp[1][i-1][par[i-1][j]].first and sp[1][i-1][j].second < sp[1][0][par[i-1][j]].second){
sp[1][i][j] = {1,sp[1][i-1][par[i-1][j]].second};
}
}
}
// for(int i = 0;i < B;i++){
// for(int j = 1;j <= n;j++){
// if(par[i][j]) cout<<i<<' '<<j<<' '<<par[i][j]<<' '<<sp[0][i][j].first<<' '<<sp[0][i][j].second<<' '<<sp[1][i][j].first<<' '<<sp[1][i][j].second<<'\n';
// }
// }
for(auto i : quer){
int tp = get<0>(i);
int x = get<1>(i);
int y = get<2>(i);
if(tp == 0){
int aa = ata(x);
int bb = ata(y);
p[bb] = aa;
}
else if(tp == 1){
int aa = ata(x);
int bb = ata(y);
if(aa != bb){
cout<<"no\n";
continue;
}
cout<<(LCA(x,y)?"yes\n":"no\n");
}
else cout<<"0\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
7104 KB |
Output is correct |
2 |
Correct |
32 ms |
7612 KB |
Output is correct |
3 |
Correct |
36 ms |
7780 KB |
Output is correct |
4 |
Correct |
28 ms |
9160 KB |
Output is correct |
5 |
Correct |
31 ms |
9336 KB |
Output is correct |
6 |
Correct |
35 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
7104 KB |
Output is correct |
2 |
Correct |
32 ms |
7612 KB |
Output is correct |
3 |
Correct |
36 ms |
7780 KB |
Output is correct |
4 |
Correct |
28 ms |
9160 KB |
Output is correct |
5 |
Correct |
31 ms |
9336 KB |
Output is correct |
6 |
Correct |
35 ms |
8908 KB |
Output is correct |
7 |
Incorrect |
23 ms |
7840 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6976 KB |
Output is correct |
2 |
Correct |
119 ms |
28488 KB |
Output is correct |
3 |
Correct |
121 ms |
28540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6976 KB |
Output is correct |
2 |
Correct |
119 ms |
28488 KB |
Output is correct |
3 |
Correct |
121 ms |
28540 KB |
Output is correct |
4 |
Incorrect |
24 ms |
7844 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
7000 KB |
Output is correct |
2 |
Correct |
117 ms |
37648 KB |
Output is correct |
3 |
Correct |
156 ms |
37568 KB |
Output is correct |
4 |
Correct |
150 ms |
47756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
7000 KB |
Output is correct |
2 |
Correct |
117 ms |
37648 KB |
Output is correct |
3 |
Correct |
156 ms |
37568 KB |
Output is correct |
4 |
Correct |
150 ms |
47756 KB |
Output is correct |
5 |
Incorrect |
27 ms |
7924 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7124 KB |
Output is correct |
2 |
Correct |
151 ms |
32440 KB |
Output is correct |
3 |
Correct |
188 ms |
33248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7124 KB |
Output is correct |
2 |
Correct |
151 ms |
32440 KB |
Output is correct |
3 |
Correct |
188 ms |
33248 KB |
Output is correct |
4 |
Incorrect |
24 ms |
7840 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
7192 KB |
Output is correct |
2 |
Correct |
138 ms |
37620 KB |
Output is correct |
3 |
Correct |
154 ms |
37644 KB |
Output is correct |
4 |
Correct |
146 ms |
47704 KB |
Output is correct |
5 |
Correct |
34 ms |
7860 KB |
Output is correct |
6 |
Correct |
167 ms |
32560 KB |
Output is correct |
7 |
Correct |
144 ms |
33408 KB |
Output is correct |
8 |
Correct |
156 ms |
33616 KB |
Output is correct |
9 |
Correct |
147 ms |
33676 KB |
Output is correct |
10 |
Correct |
184 ms |
59468 KB |
Output is correct |
11 |
Correct |
172 ms |
57096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
7192 KB |
Output is correct |
2 |
Correct |
138 ms |
37620 KB |
Output is correct |
3 |
Correct |
154 ms |
37644 KB |
Output is correct |
4 |
Correct |
146 ms |
47704 KB |
Output is correct |
5 |
Correct |
34 ms |
7860 KB |
Output is correct |
6 |
Correct |
167 ms |
32560 KB |
Output is correct |
7 |
Correct |
144 ms |
33408 KB |
Output is correct |
8 |
Correct |
156 ms |
33616 KB |
Output is correct |
9 |
Correct |
147 ms |
33676 KB |
Output is correct |
10 |
Correct |
184 ms |
59468 KB |
Output is correct |
11 |
Correct |
172 ms |
57096 KB |
Output is correct |
12 |
Incorrect |
24 ms |
7920 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7168 KB |
Output is correct |
2 |
Correct |
33 ms |
7760 KB |
Output is correct |
3 |
Correct |
94 ms |
7952 KB |
Output is correct |
4 |
Correct |
37 ms |
9152 KB |
Output is correct |
5 |
Correct |
35 ms |
9360 KB |
Output is correct |
6 |
Correct |
39 ms |
8920 KB |
Output is correct |
7 |
Correct |
30 ms |
7852 KB |
Output is correct |
8 |
Correct |
167 ms |
28568 KB |
Output is correct |
9 |
Correct |
188 ms |
28524 KB |
Output is correct |
10 |
Correct |
24 ms |
7924 KB |
Output is correct |
11 |
Correct |
252 ms |
37628 KB |
Output is correct |
12 |
Correct |
139 ms |
37664 KB |
Output is correct |
13 |
Correct |
143 ms |
47904 KB |
Output is correct |
14 |
Correct |
26 ms |
7844 KB |
Output is correct |
15 |
Correct |
134 ms |
32440 KB |
Output is correct |
16 |
Correct |
148 ms |
33280 KB |
Output is correct |
17 |
Correct |
135 ms |
33684 KB |
Output is correct |
18 |
Correct |
157 ms |
33708 KB |
Output is correct |
19 |
Correct |
162 ms |
59448 KB |
Output is correct |
20 |
Correct |
175 ms |
57060 KB |
Output is correct |
21 |
Correct |
153 ms |
30988 KB |
Output is correct |
22 |
Correct |
130 ms |
31080 KB |
Output is correct |
23 |
Correct |
150 ms |
33184 KB |
Output is correct |
24 |
Correct |
160 ms |
33672 KB |
Output is correct |
25 |
Correct |
159 ms |
33972 KB |
Output is correct |
26 |
Correct |
129 ms |
33848 KB |
Output is correct |
27 |
Correct |
112 ms |
33540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7168 KB |
Output is correct |
2 |
Correct |
33 ms |
7760 KB |
Output is correct |
3 |
Correct |
94 ms |
7952 KB |
Output is correct |
4 |
Correct |
37 ms |
9152 KB |
Output is correct |
5 |
Correct |
35 ms |
9360 KB |
Output is correct |
6 |
Correct |
39 ms |
8920 KB |
Output is correct |
7 |
Correct |
30 ms |
7852 KB |
Output is correct |
8 |
Correct |
167 ms |
28568 KB |
Output is correct |
9 |
Correct |
188 ms |
28524 KB |
Output is correct |
10 |
Correct |
24 ms |
7924 KB |
Output is correct |
11 |
Correct |
252 ms |
37628 KB |
Output is correct |
12 |
Correct |
139 ms |
37664 KB |
Output is correct |
13 |
Correct |
143 ms |
47904 KB |
Output is correct |
14 |
Correct |
26 ms |
7844 KB |
Output is correct |
15 |
Correct |
134 ms |
32440 KB |
Output is correct |
16 |
Correct |
148 ms |
33280 KB |
Output is correct |
17 |
Correct |
135 ms |
33684 KB |
Output is correct |
18 |
Correct |
157 ms |
33708 KB |
Output is correct |
19 |
Correct |
162 ms |
59448 KB |
Output is correct |
20 |
Correct |
175 ms |
57060 KB |
Output is correct |
21 |
Correct |
153 ms |
30988 KB |
Output is correct |
22 |
Correct |
130 ms |
31080 KB |
Output is correct |
23 |
Correct |
150 ms |
33184 KB |
Output is correct |
24 |
Correct |
160 ms |
33672 KB |
Output is correct |
25 |
Correct |
159 ms |
33972 KB |
Output is correct |
26 |
Correct |
129 ms |
33848 KB |
Output is correct |
27 |
Correct |
112 ms |
33540 KB |
Output is correct |
28 |
Incorrect |
24 ms |
7868 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |