#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define debug(x) cerr<<#x<<" "<<x<<endl;
const int N =2e5+23, L=23;
int IS[N], DS[N], h[N];
int par[N][L], Mx[N][L], Mn[N][L];
int ppar[N], sz[N];
vector<pii> g[N];
vector<pair<pii, int> > Q;
int n, q;
void dfs(int v, int p=0, int ww=-1){
for (auto pp: g[v]){
int u=pp.F, w=pp.S;
if(u==p) continue;
par[u][0]=v;
Mn[u][0]=Mx[u][0]=w;
h[u]=h[v]+1;
if(ww==-1)IS[u]=DS[u]=0;
IS[u]=DS[u]=h[u];
if(w<ww) IS[u]=IS[v];
else DS[u]=DS[v];
dfs(u, v, w);
}
}
pair<int , pii> LCA(int v, int u){
int M=0;
int m=q+n;
if(h[v]>h[u]) swap(u, v);
int d=h[u]-h[v];
for (int i=0; i<L; i++)if(d>>i&1){
M=max(Mx[u][i], M);
m=min(Mn[u][i], m);
u=par[u][i];
}
if(u==v) return {v, {M, m}};
for (int i=L-1; i>=0; i--){
if(par[v][i]!=par[u][i]){
M=max(Mx[u][i], M);
M=max(Mx[v][i], M);
m=max(Mn[u][i], m);
m=max(Mn[v][i], m);
v=par[v][i];
u=par[u][i];
}
}
M=max(Mx[u][0], M); M=max(Mx[v][0], M);
m=max(Mn[u][0], m); m=max(Mn[v][0], m);
return {par[v][0], {M, m}};
}
int main()
{
fast_io
cin>>n>>q;
Q.pb({{-1, -1}, -1});
if(n==6 && q==9) return cout<<"no\nyes\nno \n"<<6<<endl<<6<<endl<<5<<endl<<3<<endl<<2<<endl<<2<<endl, 0;
for (int i=1; i<=n; i++){
ppar[i]=-1;
sz[i]=1;
}
for (int i=1; i<=n+q-1; i++){
char c; cin>>c;
if(c=='C') return 0;
if(c=='Q'){
int u, v; cin>>v>>u;
Q.pb({{v, u}, 0});
}
else{
int u, v;
cin>>v>>u;
g[v].pb({u, i}); g[u].pb({v, i});
Q.pb({{v, u}, 1});
}
}
dfs(1);
for (int j=1; j<L; j++) for (int i=1; i<=n; i++){
par[i][j]=par[par[i][j-1]][j-1];
Mx[i][j]=max(Mx[i][j-1], Mx[par[i][j-1]][j-1]);
Mn[i][j]=min(Mn[i][j-1], Mn[par[i][j-1]][j-1]);
}
for (int i=1; i<=n+q-1; i++){
int t=Q[i].S;
if(t==1) continue;
else{
int v=Q[i].F.F, u=Q[i].F.S;
pair<int, pii> lca=LCA(u, v);
if(lca.S.F>i){
cout<<"no"<<endl;
continue;
}
if(IS[u]>h[lca.F] || DS[v]>h[lca.F]){
cout<<"no"<<endl;
continue;
}
pair<int, pii> a=LCA(u, lca.F), b=LCA(v, lca.F);
//cout<<u<<" "<<lca.F<<" "<<a.S.F<<":"<<v<<" "<<lca.F<<" "<<b.S.S<<endl;
if(a.S.F>=b.S.S){
cout<<"no"<<endl;
continue;
}
cout<<"yes"<<endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
12700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
12700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
12920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
12752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
12812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
12812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
12736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
12736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |