#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
// #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 3e5 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}
int fen[maxn];
vector<int> his;
void upd(int i, bool add = true){
if(add) his.pb(i);
for(i++; i < maxn; i += i&-i) fen[i] += add?1:-1;
}
int get(int i){
int res = 0;
for(i++; i; i -= i&-i) res += fen[i];
return res;
}
vector<pp> adj[maxn];
vector<pp> query1[maxn];
vector<int> query2[maxn];
int cnt[maxn], id[maxn], tp[maxn], ans[maxn], n;
bool ban[maxn], rc[maxn], cr[maxn];
void dfs(int r, int p = -1, bool st = true){ //
cnt[r] = st?1:-1;
for(auto[c, t]: adj[r]) if(!ban[c] && c - p) dfs(c, r, st), cnt[r] += st?cnt[c]:0;
}
int find_cent(int r, int p, int tot){ //
for(auto[c, t]: adj[r]) if(c - p && !ban[c] && (cnt[c]<<1) > tot) return find_cent(c, r, tot);
return r;
}
void dfs2(int r, int p, int ID){
id[r] = ID;
if(r - ID){
rc[r] = rc[p]&(tp[r] > tp[p]);
cr[r] = cr[p]&(tp[r] < tp[p]);
}
for(auto[c, t]: adj[r]) if(c - p && !ban[c]) tp[c] = t, dfs2(c, r, ID);
}
vector<int> tmp;
void dfs3(int r, int p){
if(rc[r]) tmp.pb(tp[r]);
for(auto[c, t]: query1[r]) if(cnt[c] + 1 && tp[id[c]] > tp[id[r]]){
ans[t] = cr[r]&rc[c]&(tp[c] < t);
}
for(int t: query2[r]) if(cr[r] && tp[id[r]] < t){
ans[t] += get(t);
}
for(auto[c, t]: adj[r]) if(c - p && !ban[c]) dfs3(c, r);
}
void reset(int r){
for(int c: his) upd(c, false);
his.clear();
dfs(r, -1, false);
ban[r] = true;
}
void dec(int r){
dfs(r);
r = find_cent(r, -1, cnt[r]);
sort(all(adj[r]), [&](pp a, pp b){ return a.ss > b.ss; });
rc[r] = cr[r] = true, id[r] = -1;
for(auto[c, t]: adj[r]) if(!ban[c]) rc[c] = cr[c] = true, tp[c] = t, dfs2(c, r, c);
upd(r);
for(auto[c, t]: adj[r]) if(!ban[c]){
dfs3(c, r);
for(int v: tmp) upd(v);
tmp.clear();
}
for(auto[c, t]: query1[r]) if(cnt[c] + 1 && rc[c] && t > tp[c]) ans[t] = true;
for(int t: query2[r]) ans[t] += get(t);
reset(r);
for(auto[c, t]: adj[r]) if(!ban[c]) dec(c);
}
int main(){ IOS();
int k; cin >> n >> k;
vector<int> ty(n+k);
rep(i,0,n+k-1){
char c; cin >> c;
if(c == 'S'){
int u, v; cin >> u >> v; u--, v--;
adj[u].pb({v, i}), adj[v].pb({u, i});
}else if(c == 'Q'){
ty[i] = 1;
int u, v; cin >> u >> v; u--, v--;
query1[v].pb({u, i});
}else{
ty[i] = 2;
int d; cin >> d; d--;
query2[d].pb(i);
}
}
dec(0);
rep(i,0,n+k-1) if(ty[i]){
if(ty[i]&1) cout << (ans[i]?"yes":"no") << '\n';
else cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
24656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
34 ms |
24656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
24648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
24648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
24656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
39 ms |
24656 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
24660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
24660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
24684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
24684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
24780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
35 ms |
24780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |