#include<bits/stdc++.h>
using namespace std;
const int maxn=120000+10;
struct yal{
int u,v,w;
int getad(int fu){
int ret=(fu^u^v);
return ret;
}
};
struct quer{
char c;
int u,v,w,res;
quer(){
res=0;
}
};
quer allq[maxn];
yal alled[maxn];
vector<int>adj[maxn];
int high[maxn],lca[20][maxn],n,k,now=0,nowq=0,timea=0,par[maxn],dpinc[maxn],dpdec[maxn],val[maxn];
pair<int,int>stf[maxn];
int root(int chi){
if(chi==par[chi])
{
return chi;
}
return par[chi]=root(par[chi]);
}
void con(int u,int v){
int rootu=root(u),rootv=root(v);
if(rootu!=rootv){
par[rootu]=rootv;
}
}
int isc(int u,int v){
int rootu=root(u),rootv=root(v);
//cout<<u<<" "<<v<<" "<<rootu<<" "<<rootv<<"\n";
if(rootu!=rootv){
return 0;
}
return 1;
}
void dfs(int u,int para=0){
if(u!=1){
if(val[u]<val[para]){
dpinc[u]=dpinc[para]+1;
dpdec[u]=1;
}
else{
dpinc[u]=1;
dpdec[u]=dpdec[para]+1;
}
}
timea++;
stf[u].first=timea;
lca[0][u]=para;
for(auto x:adj[u]){
int v=alled[x].getad(u);
if(v!=para){
val[v]=alled[x].w;
high[v]=high[u]+1;
dfs(v,u);
}
}
stf[u].second=timea;
}
void callca(){
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
lca[i][j]=lca[i-1][lca[i-1][j]];
}
}
}
bool zird(int u,int v){
if(stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second){
return 1;
}
return 0;
}
void solve(int ind){
int u=allq[ind].u,v=allq[ind].v;
if(zird(u,v)){
int di=high[u]-high[v];
if(dpdec[u]>=di){
return ;
}
else{
allq[ind].res=0;
return ;
}
}
if(zird(v,u)){
int di=high[v]-high[u];
if(dpinc[v]>=di){
return ;
}
else{
allq[ind].res=0;
return ;
}
}
int fu=u,fv=v;
for(int i=19;i>=0;i--){
if(lca[i][u]!=0&&!zird(v,lca[i][u])){
u=lca[i][u];
}
}
u=lca[0][u];
int di=high[fv]-high[u];
if(dpinc[fv]<di){
allq[ind].res=0;
return ;
}
di=high[fu]-high[u];
if(dpdec[fu]<di){
allq[ind].res=0;
return ;
}
if(stf[fv].first>stf[fu].first){
allq[ind].res=0;
}
};
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for(int i=1;i<maxn;i++){
par[i]=i;
}
cin>>n>>k;
for(int i=0;i<n+k-1;i++){
char c;
cin>>c;
if(c=='S'){
now++;
cin>>alled[now].u>>alled[now].v;
alled[now].w=now;
adj[alled[now].u].push_back(now);
adj[alled[now].v].push_back(now);
con(alled[now].u,alled[now].v);
continue;
}
if(c=='Q'){
nowq++;
allq[nowq].c=c;
cin>>allq[nowq].u>>allq[nowq].v;
allq[nowq].w=now;
allq[nowq].res=isc(allq[nowq].u,allq[nowq].v);
continue;
}
return 0;
nowq++;
allq[nowq].c=c;
cin>>allq[nowq].u;
allq[nowq].w=now;
}
dfs(1);
callca();
for(int i=1;i<=nowq;i++){
if(allq[i].c=='Q'){
solve(i);
if(allq[i].res==1){
cout<<"yes\n";
}
else{
cout<<"no\n";
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
7252 KB |
Output is correct |
2 |
Correct |
36 ms |
8372 KB |
Output is correct |
3 |
Correct |
37 ms |
8544 KB |
Output is correct |
4 |
Correct |
40 ms |
8468 KB |
Output is correct |
5 |
Correct |
27 ms |
8652 KB |
Output is correct |
6 |
Correct |
33 ms |
8388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
7252 KB |
Output is correct |
2 |
Correct |
36 ms |
8372 KB |
Output is correct |
3 |
Correct |
37 ms |
8544 KB |
Output is correct |
4 |
Correct |
40 ms |
8468 KB |
Output is correct |
5 |
Correct |
27 ms |
8652 KB |
Output is correct |
6 |
Correct |
33 ms |
8388 KB |
Output is correct |
7 |
Partially correct |
3 ms |
5992 KB |
Output is incorrect |
8 |
Partially correct |
3 ms |
5984 KB |
Output is incorrect |
9 |
Partially correct |
4 ms |
6100 KB |
Output is incorrect |
10 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
11 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
12 |
Partially correct |
3 ms |
5988 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7360 KB |
Output is correct |
2 |
Correct |
108 ms |
26948 KB |
Output is correct |
3 |
Correct |
112 ms |
27040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7360 KB |
Output is correct |
2 |
Correct |
108 ms |
26948 KB |
Output is correct |
3 |
Correct |
112 ms |
27040 KB |
Output is correct |
4 |
Partially correct |
4 ms |
5972 KB |
Output is incorrect |
5 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
6 |
Partially correct |
3 ms |
6020 KB |
Output is incorrect |
7 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
7316 KB |
Output is correct |
2 |
Correct |
113 ms |
36436 KB |
Output is correct |
3 |
Correct |
118 ms |
36448 KB |
Output is correct |
4 |
Correct |
75 ms |
36380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
7316 KB |
Output is correct |
2 |
Correct |
113 ms |
36436 KB |
Output is correct |
3 |
Correct |
118 ms |
36448 KB |
Output is correct |
4 |
Correct |
75 ms |
36380 KB |
Output is correct |
5 |
Partially correct |
4 ms |
5972 KB |
Output is incorrect |
6 |
Partially correct |
3 ms |
5988 KB |
Output is incorrect |
7 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
8 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
9 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
7304 KB |
Output is correct |
2 |
Correct |
107 ms |
27060 KB |
Output is correct |
3 |
Correct |
138 ms |
26932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
7304 KB |
Output is correct |
2 |
Correct |
107 ms |
27060 KB |
Output is correct |
3 |
Correct |
138 ms |
26932 KB |
Output is correct |
4 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
5 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
6 |
Partially correct |
30 ms |
10192 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
7236 KB |
Output is correct |
2 |
Correct |
111 ms |
36372 KB |
Output is correct |
3 |
Correct |
104 ms |
36452 KB |
Output is correct |
4 |
Correct |
75 ms |
36368 KB |
Output is correct |
5 |
Correct |
25 ms |
7236 KB |
Output is correct |
6 |
Correct |
100 ms |
27104 KB |
Output is correct |
7 |
Correct |
120 ms |
26960 KB |
Output is correct |
8 |
Correct |
146 ms |
27596 KB |
Output is correct |
9 |
Correct |
141 ms |
27536 KB |
Output is correct |
10 |
Correct |
133 ms |
30580 KB |
Output is correct |
11 |
Correct |
169 ms |
29828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
7236 KB |
Output is correct |
2 |
Correct |
111 ms |
36372 KB |
Output is correct |
3 |
Correct |
104 ms |
36452 KB |
Output is correct |
4 |
Correct |
75 ms |
36368 KB |
Output is correct |
5 |
Correct |
25 ms |
7236 KB |
Output is correct |
6 |
Correct |
100 ms |
27104 KB |
Output is correct |
7 |
Correct |
120 ms |
26960 KB |
Output is correct |
8 |
Correct |
146 ms |
27596 KB |
Output is correct |
9 |
Correct |
141 ms |
27536 KB |
Output is correct |
10 |
Correct |
133 ms |
30580 KB |
Output is correct |
11 |
Correct |
169 ms |
29828 KB |
Output is correct |
12 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
13 |
Partially correct |
3 ms |
5864 KB |
Output is incorrect |
14 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
15 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
16 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
17 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
18 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
19 |
Partially correct |
33 ms |
9944 KB |
Output is incorrect |
20 |
Partially correct |
3 ms |
5976 KB |
Output is incorrect |
21 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
22 |
Partially correct |
3 ms |
5976 KB |
Output is incorrect |
23 |
Partially correct |
35 ms |
10468 KB |
Output is incorrect |
24 |
Partially correct |
43 ms |
11312 KB |
Output is incorrect |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7224 KB |
Output is correct |
2 |
Correct |
39 ms |
8336 KB |
Output is correct |
3 |
Correct |
35 ms |
8448 KB |
Output is correct |
4 |
Correct |
42 ms |
8396 KB |
Output is correct |
5 |
Correct |
29 ms |
8660 KB |
Output is correct |
6 |
Correct |
32 ms |
8428 KB |
Output is correct |
7 |
Correct |
24 ms |
7364 KB |
Output is correct |
8 |
Correct |
110 ms |
26948 KB |
Output is correct |
9 |
Correct |
110 ms |
26948 KB |
Output is correct |
10 |
Correct |
23 ms |
7248 KB |
Output is correct |
11 |
Correct |
105 ms |
36428 KB |
Output is correct |
12 |
Correct |
114 ms |
36436 KB |
Output is correct |
13 |
Correct |
75 ms |
36384 KB |
Output is correct |
14 |
Correct |
25 ms |
7244 KB |
Output is correct |
15 |
Correct |
95 ms |
26984 KB |
Output is correct |
16 |
Correct |
118 ms |
27120 KB |
Output is correct |
17 |
Correct |
151 ms |
27512 KB |
Output is correct |
18 |
Correct |
136 ms |
27584 KB |
Output is correct |
19 |
Correct |
127 ms |
30540 KB |
Output is correct |
20 |
Correct |
185 ms |
29840 KB |
Output is correct |
21 |
Correct |
132 ms |
27544 KB |
Output is correct |
22 |
Correct |
128 ms |
27584 KB |
Output is correct |
23 |
Correct |
153 ms |
27572 KB |
Output is correct |
24 |
Correct |
151 ms |
27728 KB |
Output is correct |
25 |
Correct |
189 ms |
29932 KB |
Output is correct |
26 |
Correct |
120 ms |
27292 KB |
Output is correct |
27 |
Correct |
118 ms |
27208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7224 KB |
Output is correct |
2 |
Correct |
39 ms |
8336 KB |
Output is correct |
3 |
Correct |
35 ms |
8448 KB |
Output is correct |
4 |
Correct |
42 ms |
8396 KB |
Output is correct |
5 |
Correct |
29 ms |
8660 KB |
Output is correct |
6 |
Correct |
32 ms |
8428 KB |
Output is correct |
7 |
Correct |
24 ms |
7364 KB |
Output is correct |
8 |
Correct |
110 ms |
26948 KB |
Output is correct |
9 |
Correct |
110 ms |
26948 KB |
Output is correct |
10 |
Correct |
23 ms |
7248 KB |
Output is correct |
11 |
Correct |
105 ms |
36428 KB |
Output is correct |
12 |
Correct |
114 ms |
36436 KB |
Output is correct |
13 |
Correct |
75 ms |
36384 KB |
Output is correct |
14 |
Correct |
25 ms |
7244 KB |
Output is correct |
15 |
Correct |
95 ms |
26984 KB |
Output is correct |
16 |
Correct |
118 ms |
27120 KB |
Output is correct |
17 |
Correct |
151 ms |
27512 KB |
Output is correct |
18 |
Correct |
136 ms |
27584 KB |
Output is correct |
19 |
Correct |
127 ms |
30540 KB |
Output is correct |
20 |
Correct |
185 ms |
29840 KB |
Output is correct |
21 |
Correct |
132 ms |
27544 KB |
Output is correct |
22 |
Correct |
128 ms |
27584 KB |
Output is correct |
23 |
Correct |
153 ms |
27572 KB |
Output is correct |
24 |
Correct |
151 ms |
27728 KB |
Output is correct |
25 |
Correct |
189 ms |
29932 KB |
Output is correct |
26 |
Correct |
120 ms |
27292 KB |
Output is correct |
27 |
Correct |
118 ms |
27208 KB |
Output is correct |
28 |
Partially correct |
4 ms |
5972 KB |
Output is incorrect |
29 |
Partially correct |
3 ms |
5980 KB |
Output is incorrect |
30 |
Partially correct |
3 ms |
6100 KB |
Output is incorrect |
31 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
32 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
33 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
34 |
Partially correct |
3 ms |
5876 KB |
Output is incorrect |
35 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
36 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
37 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
38 |
Partially correct |
3 ms |
5992 KB |
Output is incorrect |
39 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
40 |
Partially correct |
3 ms |
5976 KB |
Output is incorrect |
41 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
42 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
43 |
Partially correct |
4 ms |
5972 KB |
Output is incorrect |
44 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
45 |
Partially correct |
27 ms |
10020 KB |
Output is incorrect |
46 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
47 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
48 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
49 |
Partially correct |
35 ms |
10480 KB |
Output is incorrect |
50 |
Partially correct |
49 ms |
11396 KB |
Output is incorrect |
51 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
52 |
Partially correct |
3 ms |
5984 KB |
Output is incorrect |
53 |
Partially correct |
3 ms |
5976 KB |
Output is incorrect |
54 |
Partially correct |
4 ms |
5972 KB |
Output is incorrect |
55 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
56 |
Partially correct |
3 ms |
5980 KB |
Output is incorrect |
57 |
Partially correct |
3 ms |
5972 KB |
Output is incorrect |
58 |
Partially correct |
38 ms |
11124 KB |
Output is incorrect |
59 |
Partially correct |
46 ms |
13064 KB |
Output is incorrect |