#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vo vector
#define pb push_back
#define se second
#define fi first
#define all(v) v.begin(), v.end()
typedef vector<int> vi;
typedef pair<int, int> pii;
#define rep(i, a, b) for(int i=(a); i<b; i++)
#define pr1(x) cerr << #x << '=' << x << ' ';
//for google contests
#define umap unordered_map
#define uset unordered_set
#define repd(i, a, b) for(int i=(b-1); i >= a; i--)
void _pr(signed x) {cerr << x;}
void _pr(long long x) {cerr << x;}
void _pr(size_t x) {cerr << x;}
void _pr(string &x) {cerr << x;}
void _pr(double x) {cerr << x;}
void _pr(char x) {cerr << '\'' << x << '\'';}
void _pr(const char* x) {cerr << x;}
void _pr(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void _pr(const pair<T, V> &x);
template<typename T, typename V> void _pr(const pair<T, V> &x) {cerr << "\e[95m" << "[ "; _pr(x.first); cerr << ", "; _pr(x.second); cerr << "\e[95m" << ']';}
template<typename T> void _pr(const T &x) {int F=0; cerr << '{'; for(auto &i: x) cerr << (F++ ? ", " : ""), _pr(i); cerr << "\e[91m" << '}';}
template <typename T, typename... V> void _pr(T t, V... v) {_pr(t); if(sizeof...(v)) cerr << ", "; _pr(v...);}
#define pr(x...) cerr << "\e[91m" << __func__ << ':' << __LINE__ << " [" << #x << "] = ["; _pr(x); cerr << "\e[91m" << ']' << "\033[0m" << endl;
mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r){return uniform_int_distribution<int>(l, r)(mt_rng);}
//go for outline with ;, then details
int const inf = 1e18, mxn = 1e5+2e4+5;
int n, q;
vo<vo<pii>> adj(mxn);
int const mxlog = 25;
int bl[mxn][mxlog], dep[mxn], paredge[mxn];
vo<vi> C;
vo<array<int, 3>> Q;
vi upDecr, upIncr;
void precomp(int at, int p, int depth, int prevedge, int incr, int decr){
sort(all(adj[at]), greater<>()); // nice properties
dep[at] = depth; bl[at][0] = p; paredge[at] = prevedge; // what about root
rep(i, 1, mxlog) bl[at][i] = bl[bl[at][i-1]][i-1];
upDecr[at] = decr, upIncr[at] = incr;
for(auto [x, ind]: adj[at]){
if(x==p) continue;
if(prevedge>ind) precomp(x, at, depth+1, ind, incr+1, 1);
else precomp(x, at, depth+1, ind, 1, decr+1);
}
}
int LCA(int a, int b){
if(dep[a] < dep[b]) swap(a, b);
int d = dep[a]-dep[b];
rep(i, 0, mxlog){
if((d>>i) & 1) a = bl[a][i];
}
if(a==b) return a;
repd(i, 0, mxlog){
if(bl[a][i] != bl[b][i]){
a = bl[a][i], b = bl[b][i];
}
}
return bl[a][0];
}
int get_lastedge(int v, int d){
d--;
rep(i, 0, mxlog){
if((d>>i) & 1) v = bl[v][i];
}
return paredge[v];
}
int vis[mxn], ans[2*mxn];
int solveQ(int a, int b, int tme){ // solve all queries of type Q
if(a==b) return 1;
int lca = LCA(a, b);
if(upIncr[b]>=dep[b]-dep[lca] && upDecr[a]>=dep[a]-dep[lca]){
if(a==lca){
int ind = get_lastedge(b, dep[b]-dep[lca]);
if(ind<tme) return 1;
}
else if(b==lca){
int ind = paredge[a];
if(ind<tme) return 1;
}
else{
int indA = get_lastedge(a, dep[a]-dep[lca]), indB = get_lastedge(b, dep[b]-dep[lca]);
if(indA>indB && paredge[a]<tme) return 1;
}
}
return 0;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>q;
upDecr.resize(n); upIncr.resize(n); C.resize(n); Q.resize(n);
int timer=0;
vi queries;
rep(i, 0, n+q-1){
char t; int a, b; cin>>t;
if(t=='S'){
cin>>a>>b; a--, b--;
adj[a].pb({b, timer}); adj[b].pb({a, timer}); queries.pb(0);
}
else if(t=='Q'){
cin>>a>>b; a--, b--; //does a store b,
Q.pb({a, b, timer});
queries.pb(1);
}
else{
cin>>a; a--;
C[a].pb(timer); queries.pb(2);
}
timer++;
}
precomp(0, 0, 0, -1, 0, 0);
vi ansQ(n+q), ansC(n+q);
for(auto [a, b, tme]: Q) ansQ[tme] = solveQ(a, b, tme);
rep(i, 0, n+q-1){
int type = queries[i];
if(type==1) cout << (ansQ[i] ? "yes" : "no") << "\n";
else cout << "-1\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |