#include "bits/stdc++.h"
//#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
const int N = 1e5 + 2e4 + 5;
const int LOG = 18;
const int INF = 1e9;
struct DSU{
vector<int> par,siz;
DSU(int n){
par.resize(n+5);
iota(all(par),0LL);
siz.assign(n+5,1LL);
}
int find(int a){
if(par[a]==a) return a;
return par[a]=find(par[a]);
}
void unite(int a,int b){
a=find(a),b=find(b);
if(a==b) return;
if(siz[a]>siz[b]) swap(a,b);
siz[b]+=siz[a];
par[a]=b;
}
};
int maxi[N][LOG],mini[N][LOG];
int lift[N][LOG],depth[N];
vector<array<int,2>> v[N];
void dfs(int c){
for(int i=1;i<LOG;i++) lift[c][i]=lift[lift[c][i-1]][i-1];
for(int i=1;i<LOG;i++){
int u = maxi[c][i-1];
int u2 = maxi[lift[c][i-1]][i-1];
if(u==-1 || u2==-1) continue;
if(u>maxi[lift[c][i-1]][0]) continue;
maxi[c][i]=u2;
}
for(int i=1;i<LOG;i++){
int u = mini[c][i-1];
int u2 = mini[lift[c][i-1]][i-1];
if(u==-1 || u2==-1) continue;
if(u<mini[lift[c][i-1]][0]) continue;
mini[c][i]=u2;
}
for(auto E:v[c]){
int x=E[0],y=E[1];
if(x==lift[c][0]) continue;
depth[x]=depth[c]+1;
lift[x][0]=c;
maxi[x][0]=mini[x][0]=y;
dfs(x);
}
}
int lca(int a,int b){
if(depth[a]<depth[b]) swap(a,b);
for(int i=0;i<LOG;i++) if((depth[a]-depth[b])>>i&1) a=lift[a][i];
if(a==b) return a;
for(int i=LOG-1;i>=0;i--){
if(lift[a][i]!=lift[b][i]){
a=lift[a][i];
b=lift[b][i];
}
}
return lift[a][0];
}
int yukari(int a,int u){
int le = depth[a]-depth[u],lol = 0;
for(int i=LOG-1;i>=0;i--){
if(!(le>>i&1)) continue;
if(maxi[a][i]==-1) return -1;
if(lol>maxi[a][0]) return -1;
lol=maxi[a][i];
a=lift[a][i];
}
return lol;
}
int asagi(int a,int u){
int le = depth[a]-depth[u],lol = INF;
for(int i=LOG-1;i>=0;i--){
if(!(le>>i&1)) continue;
if(mini[a][i]==-1) return -1;
if(lol<mini[a][0]) return -1;
lol=mini[a][i];
a=lift[a][i];
}
return lol;
}
bool check(int a,int b){
if(a==b) return true;
int c=lca(a,b);
int hm = yukari(a,c);
int hm2 = asagi(b,c);
if(hm==-1 || hm2==-1) return false;
return hm<=hm2;
}
void _(){
memset(maxi,-1,sizeof(maxi));
memset(mini,-1,sizeof(mini));
int n,q;
cin >> n >> q;
DSU dsu(n+5);
vector<bool> ans(q+5,1);
vector<array<int,2>> bak(q+5);
int cp=0;
for(int i=1;i<n+q;i++){
char op; cin >> op;
int a,b;
cin >> a >> b;
if(op=='S'){
v[a].push_back({b,i});
v[b].push_back({a,i});
dsu.unite(a,b);
}
else{
cp++;
bak[cp]={a,b};
if(dsu.find(a)!=dsu.find(b)) ans[cp]=0;
}
}
dfs(1);
for(int i=1;i<=q;i++){
if(!ans[i]) continue;
int a=bak[i][0],b=bak[i][1];
ans[i]=check(b,a);
}
for(int i=1;i<=q;i++){
if(ans[i]) cout << "yes\n";
else cout << "no\n";
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int tc=1;//cin >> tc;
while(tc--) _();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
24928 KB |
Output is correct |
2 |
Correct |
22 ms |
25684 KB |
Output is correct |
3 |
Correct |
29 ms |
25676 KB |
Output is correct |
4 |
Correct |
22 ms |
25932 KB |
Output is correct |
5 |
Correct |
34 ms |
25936 KB |
Output is correct |
6 |
Correct |
28 ms |
25684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
24928 KB |
Output is correct |
2 |
Correct |
22 ms |
25684 KB |
Output is correct |
3 |
Correct |
29 ms |
25676 KB |
Output is correct |
4 |
Correct |
22 ms |
25932 KB |
Output is correct |
5 |
Correct |
34 ms |
25936 KB |
Output is correct |
6 |
Correct |
28 ms |
25684 KB |
Output is correct |
7 |
Incorrect |
9 ms |
24148 KB |
Extra information in the output file |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25060 KB |
Output is correct |
2 |
Correct |
81 ms |
38848 KB |
Output is correct |
3 |
Correct |
88 ms |
38796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25060 KB |
Output is correct |
2 |
Correct |
81 ms |
38848 KB |
Output is correct |
3 |
Correct |
88 ms |
38796 KB |
Output is correct |
4 |
Incorrect |
9 ms |
24148 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
25040 KB |
Output is correct |
2 |
Correct |
118 ms |
49752 KB |
Output is correct |
3 |
Correct |
133 ms |
49636 KB |
Output is correct |
4 |
Correct |
126 ms |
49544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
25040 KB |
Output is correct |
2 |
Correct |
118 ms |
49752 KB |
Output is correct |
3 |
Correct |
133 ms |
49636 KB |
Output is correct |
4 |
Correct |
126 ms |
49544 KB |
Output is correct |
5 |
Incorrect |
10 ms |
24148 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24908 KB |
Output is correct |
2 |
Correct |
89 ms |
39132 KB |
Output is correct |
3 |
Correct |
112 ms |
39892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
24908 KB |
Output is correct |
2 |
Correct |
89 ms |
39132 KB |
Output is correct |
3 |
Correct |
112 ms |
39892 KB |
Output is correct |
4 |
Incorrect |
10 ms |
24148 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25172 KB |
Output is correct |
2 |
Correct |
120 ms |
49740 KB |
Output is correct |
3 |
Correct |
116 ms |
49720 KB |
Output is correct |
4 |
Correct |
131 ms |
49484 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
84 ms |
39332 KB |
Output is correct |
7 |
Correct |
84 ms |
39784 KB |
Output is correct |
8 |
Correct |
135 ms |
40096 KB |
Output is correct |
9 |
Correct |
117 ms |
40276 KB |
Output is correct |
10 |
Correct |
136 ms |
45388 KB |
Output is correct |
11 |
Correct |
152 ms |
44604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25172 KB |
Output is correct |
2 |
Correct |
120 ms |
49740 KB |
Output is correct |
3 |
Correct |
116 ms |
49720 KB |
Output is correct |
4 |
Correct |
131 ms |
49484 KB |
Output is correct |
5 |
Correct |
19 ms |
24916 KB |
Output is correct |
6 |
Correct |
84 ms |
39332 KB |
Output is correct |
7 |
Correct |
84 ms |
39784 KB |
Output is correct |
8 |
Correct |
135 ms |
40096 KB |
Output is correct |
9 |
Correct |
117 ms |
40276 KB |
Output is correct |
10 |
Correct |
136 ms |
45388 KB |
Output is correct |
11 |
Correct |
152 ms |
44604 KB |
Output is correct |
12 |
Incorrect |
10 ms |
24148 KB |
Extra information in the output file |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25172 KB |
Output is correct |
2 |
Correct |
24 ms |
25684 KB |
Output is correct |
3 |
Correct |
33 ms |
25760 KB |
Output is correct |
4 |
Correct |
29 ms |
25932 KB |
Output is correct |
5 |
Correct |
35 ms |
26188 KB |
Output is correct |
6 |
Correct |
38 ms |
25736 KB |
Output is correct |
7 |
Correct |
21 ms |
25172 KB |
Output is correct |
8 |
Correct |
89 ms |
38848 KB |
Output is correct |
9 |
Correct |
101 ms |
38848 KB |
Output is correct |
10 |
Correct |
21 ms |
25172 KB |
Output is correct |
11 |
Correct |
118 ms |
49740 KB |
Output is correct |
12 |
Correct |
108 ms |
49748 KB |
Output is correct |
13 |
Correct |
107 ms |
49488 KB |
Output is correct |
14 |
Correct |
20 ms |
24920 KB |
Output is correct |
15 |
Correct |
86 ms |
39244 KB |
Output is correct |
16 |
Correct |
97 ms |
39748 KB |
Output is correct |
17 |
Correct |
132 ms |
40280 KB |
Output is correct |
18 |
Correct |
107 ms |
40276 KB |
Output is correct |
19 |
Correct |
130 ms |
45388 KB |
Output is correct |
20 |
Correct |
148 ms |
44456 KB |
Output is correct |
21 |
Correct |
100 ms |
39484 KB |
Output is correct |
22 |
Correct |
98 ms |
39348 KB |
Output is correct |
23 |
Correct |
115 ms |
39740 KB |
Output is correct |
24 |
Correct |
124 ms |
39756 KB |
Output is correct |
25 |
Correct |
115 ms |
41976 KB |
Output is correct |
26 |
Correct |
106 ms |
39244 KB |
Output is correct |
27 |
Correct |
95 ms |
39244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
25172 KB |
Output is correct |
2 |
Correct |
24 ms |
25684 KB |
Output is correct |
3 |
Correct |
33 ms |
25760 KB |
Output is correct |
4 |
Correct |
29 ms |
25932 KB |
Output is correct |
5 |
Correct |
35 ms |
26188 KB |
Output is correct |
6 |
Correct |
38 ms |
25736 KB |
Output is correct |
7 |
Correct |
21 ms |
25172 KB |
Output is correct |
8 |
Correct |
89 ms |
38848 KB |
Output is correct |
9 |
Correct |
101 ms |
38848 KB |
Output is correct |
10 |
Correct |
21 ms |
25172 KB |
Output is correct |
11 |
Correct |
118 ms |
49740 KB |
Output is correct |
12 |
Correct |
108 ms |
49748 KB |
Output is correct |
13 |
Correct |
107 ms |
49488 KB |
Output is correct |
14 |
Correct |
20 ms |
24920 KB |
Output is correct |
15 |
Correct |
86 ms |
39244 KB |
Output is correct |
16 |
Correct |
97 ms |
39748 KB |
Output is correct |
17 |
Correct |
132 ms |
40280 KB |
Output is correct |
18 |
Correct |
107 ms |
40276 KB |
Output is correct |
19 |
Correct |
130 ms |
45388 KB |
Output is correct |
20 |
Correct |
148 ms |
44456 KB |
Output is correct |
21 |
Correct |
100 ms |
39484 KB |
Output is correct |
22 |
Correct |
98 ms |
39348 KB |
Output is correct |
23 |
Correct |
115 ms |
39740 KB |
Output is correct |
24 |
Correct |
124 ms |
39756 KB |
Output is correct |
25 |
Correct |
115 ms |
41976 KB |
Output is correct |
26 |
Correct |
106 ms |
39244 KB |
Output is correct |
27 |
Correct |
95 ms |
39244 KB |
Output is correct |
28 |
Incorrect |
10 ms |
24148 KB |
Extra information in the output file |
29 |
Halted |
0 ms |
0 KB |
- |