#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e)?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
struct DSU{
int p[120005];
DSU(){
rep(x,0,120005) p[x]=x;
}
int par(int i){
if (p[i]==i) return i;
else return p[i]=par(p[i]);
}
void unions(int i,int j){
i=par(i),j=par(j);
p[i]=j;
}
} dsu=DSU();
int n,k;
vector<ii> al[120005];
int d[120005];
int tkd[120005][20];
int asc[120005];
int dsc[120005];
int w[120005];
void dfs(int i,int p){
for (auto &it:al[i]){
if (it.fi==p) continue;
w[it.fi]=it.se;
asc[it.fi]=dsc[it.fi]=i;
if (w[it.fi]>w[i]) asc[it.fi]=asc[i];
else dsc[it.fi]=dsc[i];
d[it.fi]=d[i]+1;
int curr=tkd[it.fi][0]=i;
rep(x,0,20){
if (curr==-1) break;
curr=tkd[it.fi][x+1]=tkd[curr][x];
}
dfs(it.fi,i);
}
}
int mov(int i,int j){
rep(bit,0,20) if (j&(1<<bit)){
i=tkd[i][bit];
}
return i;
}
int lca(int i,int j){
if (d[i]<d[j]) swap(i,j);
i=mov(i,d[i]-d[j]);
if (i==j) return i;
rep(x,20,0){
if (tkd[i][x]!=tkd[j][x]) i=tkd[i][x],j=tkd[j][x];
}
return tkd[i][0];
}
struct dat{
char c;
int a,b;
};
vector<dat> Q;
bool reach(int a,int b){
if (dsu.par(a)!=dsu.par(b)) return false;
int g=lca(a,b);
if (g==a){
if (d[dsc[b]]<=d[g]) return true;
else return false;
}
else if (g==b){
if (d[asc[a]]<=d[g]) return true;
else return false;
}
else{
bool can=true;
if (d[dsc[b]]>d[g]) can=false;
if (d[asc[a]]>d[g]) can=false;
b=mov(b,d[b]-d[g]-1);
a=mov(a,d[a]-d[g]-1);
if (w[a]<w[b]) can=false;
return can;
}
}
int add(int i,int j){
return i+j;
}
int main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n>>k;
char c;
int a,b;
int IDX=0;
rep(x,0,n+k-1){
cin>>c;
if (c=='S'){
cin>>a>>b;
Q.pub({c,a,b});
al[a].pub(ii(b,IDX));
al[b].pub(ii(a,IDX));
IDX++;
}
else if (c=='Q'){
cin>>a>>b;
Q.pub({c,a,b});
}
else{
cin>>a;
Q.pub({c,a,0});
}
}
memset(tkd,-1,sizeof(tkd));
asc[1]=dsc[1]=1;
dfs(1,-1);
//rep(x,1,n+1) cout<<w[x]<<" "; cout<<endl;
//rep(x,1,n+1) cout<<asc[x]<<" "; cout<<endl;
//rep(x,1,n+1) cout<<dsc[x]<<" "; cout<<endl;
//rep(x,1,n+1) cout<<d[x]<<" "; cout<<endl;
//rep(x,1,n+1) cout<<in[x]<<" "; cout<<endl;
//rep(x,1,n+1) cout<<out[x]<<" "; cout<<endl;
for (auto &it:Q){
if (it.c=='S'){
dsu.unions(it.a,it.b);
}
else if (it.c=='Q'){
if (reach(it.a,it.b)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
else{
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14904 KB |
Output is correct |
2 |
Correct |
48 ms |
16524 KB |
Output is correct |
3 |
Correct |
61 ms |
16484 KB |
Output is correct |
4 |
Correct |
43 ms |
16452 KB |
Output is correct |
5 |
Correct |
54 ms |
16776 KB |
Output is correct |
6 |
Correct |
63 ms |
16436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14904 KB |
Output is correct |
2 |
Correct |
48 ms |
16524 KB |
Output is correct |
3 |
Correct |
61 ms |
16484 KB |
Output is correct |
4 |
Correct |
43 ms |
16452 KB |
Output is correct |
5 |
Correct |
54 ms |
16776 KB |
Output is correct |
6 |
Correct |
63 ms |
16436 KB |
Output is correct |
7 |
Incorrect |
39 ms |
15680 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
14900 KB |
Output is correct |
2 |
Correct |
153 ms |
25620 KB |
Output is correct |
3 |
Correct |
157 ms |
25580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
14900 KB |
Output is correct |
2 |
Correct |
153 ms |
25620 KB |
Output is correct |
3 |
Correct |
157 ms |
25580 KB |
Output is correct |
4 |
Incorrect |
42 ms |
15672 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
14904 KB |
Output is correct |
2 |
Correct |
144 ms |
31232 KB |
Output is correct |
3 |
Correct |
149 ms |
31396 KB |
Output is correct |
4 |
Correct |
136 ms |
31216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
14904 KB |
Output is correct |
2 |
Correct |
144 ms |
31232 KB |
Output is correct |
3 |
Correct |
149 ms |
31396 KB |
Output is correct |
4 |
Correct |
136 ms |
31216 KB |
Output is correct |
5 |
Incorrect |
37 ms |
14812 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14864 KB |
Output is correct |
2 |
Correct |
141 ms |
26096 KB |
Output is correct |
3 |
Correct |
143 ms |
26552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
14864 KB |
Output is correct |
2 |
Correct |
141 ms |
26096 KB |
Output is correct |
3 |
Correct |
143 ms |
26552 KB |
Output is correct |
4 |
Incorrect |
38 ms |
15676 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
14892 KB |
Output is correct |
2 |
Correct |
141 ms |
31216 KB |
Output is correct |
3 |
Correct |
145 ms |
31192 KB |
Output is correct |
4 |
Correct |
137 ms |
31208 KB |
Output is correct |
5 |
Correct |
43 ms |
14844 KB |
Output is correct |
6 |
Correct |
149 ms |
25968 KB |
Output is correct |
7 |
Correct |
153 ms |
26476 KB |
Output is correct |
8 |
Correct |
201 ms |
26864 KB |
Output is correct |
9 |
Correct |
163 ms |
26884 KB |
Output is correct |
10 |
Correct |
163 ms |
31484 KB |
Output is correct |
11 |
Correct |
228 ms |
30808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
14892 KB |
Output is correct |
2 |
Correct |
141 ms |
31216 KB |
Output is correct |
3 |
Correct |
145 ms |
31192 KB |
Output is correct |
4 |
Correct |
137 ms |
31208 KB |
Output is correct |
5 |
Correct |
43 ms |
14844 KB |
Output is correct |
6 |
Correct |
149 ms |
25968 KB |
Output is correct |
7 |
Correct |
153 ms |
26476 KB |
Output is correct |
8 |
Correct |
201 ms |
26864 KB |
Output is correct |
9 |
Correct |
163 ms |
26884 KB |
Output is correct |
10 |
Correct |
163 ms |
31484 KB |
Output is correct |
11 |
Correct |
228 ms |
30808 KB |
Output is correct |
12 |
Incorrect |
37 ms |
15744 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
14904 KB |
Output is correct |
2 |
Correct |
47 ms |
16324 KB |
Output is correct |
3 |
Correct |
63 ms |
16564 KB |
Output is correct |
4 |
Correct |
44 ms |
16456 KB |
Output is correct |
5 |
Correct |
57 ms |
16792 KB |
Output is correct |
6 |
Correct |
63 ms |
16388 KB |
Output is correct |
7 |
Correct |
43 ms |
15800 KB |
Output is correct |
8 |
Correct |
155 ms |
25640 KB |
Output is correct |
9 |
Correct |
149 ms |
25584 KB |
Output is correct |
10 |
Correct |
38 ms |
15732 KB |
Output is correct |
11 |
Correct |
142 ms |
34564 KB |
Output is correct |
12 |
Correct |
146 ms |
34656 KB |
Output is correct |
13 |
Correct |
141 ms |
34412 KB |
Output is correct |
14 |
Correct |
39 ms |
15720 KB |
Output is correct |
15 |
Correct |
148 ms |
26228 KB |
Output is correct |
16 |
Correct |
147 ms |
26464 KB |
Output is correct |
17 |
Correct |
192 ms |
26908 KB |
Output is correct |
18 |
Correct |
169 ms |
27028 KB |
Output is correct |
19 |
Correct |
165 ms |
31428 KB |
Output is correct |
20 |
Correct |
219 ms |
30828 KB |
Output is correct |
21 |
Correct |
159 ms |
26164 KB |
Output is correct |
22 |
Correct |
147 ms |
26220 KB |
Output is correct |
23 |
Correct |
163 ms |
26436 KB |
Output is correct |
24 |
Correct |
169 ms |
26656 KB |
Output is correct |
25 |
Correct |
165 ms |
28328 KB |
Output is correct |
26 |
Correct |
143 ms |
26012 KB |
Output is correct |
27 |
Correct |
131 ms |
25980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
14904 KB |
Output is correct |
2 |
Correct |
47 ms |
16324 KB |
Output is correct |
3 |
Correct |
63 ms |
16564 KB |
Output is correct |
4 |
Correct |
44 ms |
16456 KB |
Output is correct |
5 |
Correct |
57 ms |
16792 KB |
Output is correct |
6 |
Correct |
63 ms |
16388 KB |
Output is correct |
7 |
Correct |
43 ms |
15800 KB |
Output is correct |
8 |
Correct |
155 ms |
25640 KB |
Output is correct |
9 |
Correct |
149 ms |
25584 KB |
Output is correct |
10 |
Correct |
38 ms |
15732 KB |
Output is correct |
11 |
Correct |
142 ms |
34564 KB |
Output is correct |
12 |
Correct |
146 ms |
34656 KB |
Output is correct |
13 |
Correct |
141 ms |
34412 KB |
Output is correct |
14 |
Correct |
39 ms |
15720 KB |
Output is correct |
15 |
Correct |
148 ms |
26228 KB |
Output is correct |
16 |
Correct |
147 ms |
26464 KB |
Output is correct |
17 |
Correct |
192 ms |
26908 KB |
Output is correct |
18 |
Correct |
169 ms |
27028 KB |
Output is correct |
19 |
Correct |
165 ms |
31428 KB |
Output is correct |
20 |
Correct |
219 ms |
30828 KB |
Output is correct |
21 |
Correct |
159 ms |
26164 KB |
Output is correct |
22 |
Correct |
147 ms |
26220 KB |
Output is correct |
23 |
Correct |
163 ms |
26436 KB |
Output is correct |
24 |
Correct |
169 ms |
26656 KB |
Output is correct |
25 |
Correct |
165 ms |
28328 KB |
Output is correct |
26 |
Correct |
143 ms |
26012 KB |
Output is correct |
27 |
Correct |
131 ms |
25980 KB |
Output is correct |
28 |
Incorrect |
38 ms |
15696 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |