#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 2.5e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int n, k;
vector<pair<int,int> > adj[MAXN];
vector<int> et;
int l[MAXN], r[MAXN], de[MAXN];
pair<int, int> spt[18][MAXN];
int par[MAXN];
int u1[MAXN],u2[MAXN];//if strictly increasing/decreasing, min [height]
int parw[MAXN];//i->par[i] weight
void dfs(int node, int prv) {
et.push_back(node);
par[node]=prv;
de[node] = (prv == -1 ? 0 : de[prv] + 1);
if(prv != -1) {
if(prv == 1) {
u1[node]=u2[node]=de[node];
}
else if(parw[node]<parw[prv]) {
u2[node]=de[node];
u1[node]=u1[prv];
}
else {
u1[node]=de[node];
u2[node]=u2[prv];
}
}
for(auto x: adj[node]) {
if(x.first!=prv) {
parw[x.first]=x.second;
dfs(x.first, node);
et.push_back(node);
}
}
}
int lca(int x, int y) {
int m1=min(l[x],l[y]), m2=max(r[x],r[y]);
int k=32-__builtin_clz(m2-m1+1)-1;
return min(spt[k][m1],spt[k][m2-(1<<k)+1]).second;
}
int dist(int x, int y) {
return de[x]+de[y]-2*de[lca(x,y)];
}
int dsu[MAXN];
int set_of(int u) {
if(dsu[u] == u) return u;
return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
dsu[set_of(u)] = set_of(v);
}
void solve(int tc) {
cin >> n >> k;
for(int i=1; i<=n; i++) dsu[i] = i;
vector<pair<pair<int, int>, int> > vt;
vector<int> oh;
for(int i=0; i<n+k-1; i++) {
char t; cin >> t;
if(t == 'S') {
int a, b;
cin >> a >> b;
adj[a].push_back({b, i});
adj[b].push_back({a, i});
union_(a, b);
}
else {
int a, d;
cin >> a >> d;
if(set_of(a) == set_of(d)) oh.push_back(1);
else oh.push_back(0);
vt.push_back({{a, d}, i});
}
}
dfs(1,-1);
for(int i=0; i<et.size(); i++) r[et[i]]=i;
for(int i=et.size()-1; i>=0; i--) l[et[i]]=i;
for(int i=0; i<et.size(); i++) spt[0][i] = {de[et[i]], et[i]};
for(int i=1; i<18; i++) {
for(int j=0; j+(1<<i)-1<et.size(); j++) spt[i][j] = min(spt[i-1][j], spt[i-1][j+(1<<(i-1))]);
}
int ptr=0;
for(auto x: vt) {
int a=x.first.first, d=x.first.second, i=x.second;
if(oh[ptr] == 0) cout << "no\n";
else if(par[a] == d || par[d] == a) cout << "yes\n";
else if(u1[d]<=de[lca(a,d)] && u2[a]<=de[lca(a,d)]) cout << "yes\n";
else cout << "no\n";
ptr++;
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
6 4
S 1 2
Q 1 3
Q 3 1
S 1 3
Q 1 3
Q 3 1
S 1 4
S 1 5
S 1 6
*/
Compilation message
servers.cpp: In function 'void solve(long long int)':
servers.cpp:96:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i=0; i<et.size(); i++) r[et[i]]=i;
| ~^~~~~~~~~~
servers.cpp:98:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int i=0; i<et.size(); i++) spt[0][i] = {de[et[i]], et[i]};
| ~^~~~~~~~~~
servers.cpp:100:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int j=0; j+(1<<i)-1<et.size(); j++) spt[i][j] = min(spt[i-1][j], spt[i-1][j+(1<<(i-1))]);
| ~~~~~~~~~~^~~~~~~~~~
servers.cpp:104:44: warning: unused variable 'i' [-Wunused-variable]
104 | int a=x.first.first, d=x.first.second, i=x.second;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
10584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
10584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
10568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
10568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
10644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
10644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
10600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
10600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
10576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
10576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |