#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int maxn = 240000+5;
struct edge{
int u, t, i;
};
vector<edge> g[maxn];
vector<int> cnt[maxn];
//vector<int> frm[maxn];
int now[maxn];
int ans[maxn];
vector<int> hey[maxn];
vector<int> hey2[maxn];
struct havequery{
int a,b,t;
};
int inc[maxn], decr[maxn];
int par[maxn], parw[maxn]; // parent and parent weight
int fa[21][maxn];
int dep[maxn];
int L[maxn], R[maxn];
int IT = 0;
bool ispar(int a, int b) {
return L[a] <= L[b] && R[a] >= R[b];
}
void dfs(int v, int p) {
L[v] =R[v]=IT++;
for (edge e : g[v]) {
int u = e.u;
if (u != p) {
fa[0][u] = v;
par[u] = v; parw[u] = e.t;
dep[u] = dep[v] + 1;
inc[u] = decr[u] = v;
if (parw[u] > parw[v]) {
inc[u] = inc[v];
}
else{
decr[u] = decr[v];
}
dfs(u,v);
R[v] = R[u];
}
}
}
int kth(int a, int k) {
for (int j = 0; j<20; ++j) {
if (k & (1<<j)) a = fa[j][a];
}
return a;
}
pii LCA(int a, int b) {
if (dep[a] < dep[b]) swap(a,b);
a = kth(a, dep[a] - dep[b]);
if (a == b) return {a,b};
for (int j = 20; j>=0; --j) {
if (fa[j][a] != fa[j][b]) {
a = fa[j][a]; b = fa[j][b];
}
}
return {a,b};
}
signed main(){
IOS();
int n,q; cin>>n>>q; q += n-1;
vector<havequery> hv;
// fill(ans, ans+maxn, 20202930);
vector<pair<pii, int> > E;
REP(i,q) {
char c; cin>>c;
if (c == 'S') {
int a,b; cin>>a>>b; --a; --b;
g[a].pb({b,i,SZ(g[a])});
g[b].pb({a,i,SZ(g[b])});
E.pb({{a,b},i});
ans[i] = 202022020;
}
if (c == 'Q') {
int a, b; cin>>a>>b; --a; --b;
hv.pb({a,b,i});
}
if (c == 'C') {
int a; cin>>a; --a;
hey[a].pb(i);
hey2[a].pb(i);
}
}
dfs(0, -1);
REP1(j, 20) REP(i,n) fa[j][i] = fa[j-1][fa[j-1][i]];
reverse(ALL(E));
fill(now, now+maxn, 1);
for( auto P : E) {
int a = P.f.f, b=P.f.s;
int tm = P.s;
bug(a,b,tm);
for (int v : {a,b}) {
while (!hey[v].empty()) {
if (hey[v].back() > tm) {
ans[hey[v].back()] -= now[v];
// bug("ayy");
hey[v].pop_back();
}else break;
}
}
int nw = now[a] + now[b];
now[a] = now[b] = nw;
}
REP(i,n) {
while (!hey[i].empty() ) {
ans[hey[i].back()] -= now[i];
hey[i].pop_back();
}
}
REP(i,n) {
for (int tt : hey2[i]) ans[tt] += 1 + now[i];
}
{
for (havequery HH : hv) {
int a = HH.a, b = HH.b;
swap(a,b); // walking from a to b!!!!!
int hoho = HH.t;
if (a == b) {
ans[hoho] = -1; continue;
}
pii lol = LCA(a,b);
int A = lol.f, B = lol.s;
int C = A==B?A:par[A];
int lstedge;
if (C == b) {
lstedge = parw[kth(a, dep[a] - dep[C] - 1)];
}else{
lstedge = parw[b];
}
bug(a,b,hoho,A,B,C,lstedge);
if (dep[inc[b]] > dep[C] || dep[decr[a]] > dep[C]) {
ans[hoho] = -2; continue;
}
if (lstedge > HH.t) {
ans[hoho] = -2; continue;
}
if (A!=B) {
if (parw[A] > parw[B]) {
ans[hoho] = -2; continue;
}
}
ans[hoho] = -1;
// is the path from a
}
}
REP(i,q) {
if (ans[i] != 0 && ans[i] < 10000000) {
if (ans[i] > 0) cout<<ans[i]<<endl;
else cout<<(ans[i] == -1?"yes":"no")<<endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
26252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
26252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
26252 KB |
Output is correct |
2 |
Correct |
332 ms |
49336 KB |
Output is correct |
3 |
Correct |
306 ms |
49240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
26252 KB |
Output is correct |
2 |
Correct |
332 ms |
49336 KB |
Output is correct |
3 |
Correct |
306 ms |
49240 KB |
Output is correct |
4 |
Incorrect |
55 ms |
26272 KB |
Extra information in the output file |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
26312 KB |
Output is correct |
2 |
Correct |
146 ms |
57720 KB |
Output is correct |
3 |
Correct |
144 ms |
57656 KB |
Output is correct |
4 |
Correct |
143 ms |
57756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
26312 KB |
Output is correct |
2 |
Correct |
146 ms |
57720 KB |
Output is correct |
3 |
Correct |
144 ms |
57656 KB |
Output is correct |
4 |
Correct |
143 ms |
57756 KB |
Output is correct |
5 |
Incorrect |
54 ms |
26296 KB |
Extra information in the output file |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
26292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
26292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
26228 KB |
Output is correct |
2 |
Correct |
156 ms |
57656 KB |
Output is correct |
3 |
Correct |
148 ms |
57636 KB |
Output is correct |
4 |
Correct |
144 ms |
57572 KB |
Output is correct |
5 |
Incorrect |
55 ms |
27188 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
26228 KB |
Output is correct |
2 |
Correct |
156 ms |
57656 KB |
Output is correct |
3 |
Correct |
148 ms |
57636 KB |
Output is correct |
4 |
Correct |
144 ms |
57572 KB |
Output is correct |
5 |
Incorrect |
55 ms |
27188 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
26260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
26260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |