#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(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) p[y] = x;
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";
}
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:142:19: warning: second operand of conditional expression has no effect [-Wunused-value]
142 | cout<<LCA(x,y)?"yes\n":"no\n";
| ^~~~~~~
servers.cpp:142:27: warning: third operand of conditional expression has no effect [-Wunused-value]
142 | cout<<LCA(x,y)?"yes\n":"no\n";
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
7960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
7960 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
7968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
7968 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
8000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
8000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
8104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
8104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
7948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
7948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
7936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
7936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |