#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
typedef pair<int,int> pii;
const int MAXN = 2e5;
vector<pii>adj[MAXN];
int dep[MAXN],par[18][MAXN],asc[MAXN],dsc[MAXN],val[MAXN],n,k;
char c[250000];
int a[250000],d[250000];
void dfs(int v,int u,int ww){
for(int i=1;i<=17;i++)par[i][v] = par[i-1][par[i-1][v]];
for(pii z:adj[v]){
int x = z.fi;
int w = z.se;
if(x == u)continue;
val[x] = w;
if(w > ww || v==1)asc[x] = asc[v];
else asc[x] = dep[v];
if(w < ww || v==1)dsc[x] = dsc[v];
else dsc[x] = dep[v];
dep[x] = dep[v]+1;
par[0][x] = v;
dfs(x,v,w);
}
}
int jump(int v,int d){
for(int i=0;i<=17 && d;i++){
if(d&1)v=par[i][v];
d/=2;
}
return v;
}
int lca(int v,int u){
if(dep[v] < dep[u])swap(v,u);
v = jump(v,dep[v] - dep[u]);
if(v==u)return v;
for(int i=17;i>=0;i--){
if(par[i][v] != par[i][u]){
v = par[i][v];
u = par[i][u];
}
}
return par[0][v];
}
int p[MAXN];
int find(int x){
if(x==p[x])return x;
else return p[x] = find(p[x]);
}
bool unite(int a,int b){
a = find(a);
b = find(b);
if(a==b)return 0;
p[a] = b;
return 1;
}
int main()
{
owo
cin>>n>>k;
int cnt = 0;
for(int i=1;i<=n;i++)p[i] = i;
for(int i=0;i<n+k-1;i++){
cin>>c[i]>>a[i];
if(c[i]!='C')cin>>d[i];
if(c[i] == 'S'){
adj[a[i]].pb({d[i],++cnt});
adj[d[i]].pb({a[i],cnt});
}
}
dfs(1,0,0);
for(int i=1;i<=n;i++)cout<<asc[i]<<" ";
cout<<'\n';
for(int i=1;i<=n;i++)cout<<dsc[i]<<" ";
cout<<'\n';
for(int i=0;i<n+k-1;i++){
if(c[i] == 'S')unite(a[i],d[i]);
else if(c[i]=='C'){
cout<<42<<'\n';
continue;
}
else{
int v = a[i];
int u = d[i];
if(find(v) != find(u)){
cout<<"no"<<'\n';
continue;
}
int m = lca(v,u);
bool ok=1;
if(asc[v] > dep[m] || dsc[u] > dep[m])ok = 0;
int b1 = 1e9;
int b2 = -1e9;
if(v!=m){
v = jump(v,dep[v]-dep[m]-1);
b1 = val[v];
}
if(u!=m){
u = jump(u,dep[u]-dep[m]-1);
b2 = val[u];
}
if(b2 > b1)ok = 0;
if(ok)cout<<"yes"<<'\n';
else cout<<"no"<<'\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
6544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
6544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
6548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
6548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
6520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
6520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
6468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
6468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
6468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
6468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
6548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
6548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |