#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 120100;
const int M = N * 2;
vector<pii> T[N];
vector<pii> qq[N];
vector<int> cc[N];
int bruh[M];
int soln[M];
bool chk(int node, int par, int target, int las){
if(node == target) return true;
for(auto x : T[node]){
if(x.fi == par || x.se < las) continue;
if(chk(x.fi, node, target, x.se))
return true;
}
return false;
}
int segt[M * 2 + 512];
int lim;
vector<pii> up;
void upd(int idx, int v, bool keep){
if(keep)up.push_back(mp(idx, v));
idx += lim;
while(idx > 0){
segt[idx] += v;
idx /= 2;
}
}
int calc(int li, int ri){
li += lim;
ri += lim;
int cum = 0;
while(li <= ri){
if(li % 2 == 1) cum += segt[li ++ ];
if(ri % 2 == 0) cum += segt[ri -- ];
li /= 2;
ri /= 2;
}
return cum;
}
int sub[N];
bool vis[N];
int dd[N];
int nani[N];
int iter;
void dfs(int u, int pp){
sub[u]=1;
dd[u]=iter;
nani[u]=-1;
for(auto x : T[u]){
if(x.fi == pp || vis[x.fi]) continue;
dfs(x.fi, u);
sub[u] += sub[x.fi];
}
}
bool ton[N];
bool fron[N];
int in[N];
int shd[N];
void dfs1(int u, int pp, int ss, int l1, int l2){
in[u]=l2;
nani[u]=ss;
if(l1 == -1 || l2 == -1){
ton[u]=true;
fron[u]=true;
}
else{
if(l1 > l2){
fron[u] = fron[pp];
ton[u] = false;
}
else{
fron[u] = false;
ton[u] = ton[pp];
}
}
for(auto x : qq[u]){
if(dd[u]!=dd[x.fi] || nani[u]==nani[x.fi]) continue;
if(nani[x.fi] == -1){
soln[x.se] = -2;
}
else{
if(nani[x.fi] == 0){
if(fron[u] && shd[ss] <= x.se){
soln[x.se] = -1;
}
else{
soln[x.se] = -2;
}
}
else{
if(fron[u] && ton[x.fi] && in[x.fi] <= x.se){
soln[x.se] = -1;
}
else{
soln[x.se] = -2;
}
}
}
}
for(auto x : cc[u]){
if(fron[u]){
soln[x] += calc(1, x);
if(shd[ss] <= x)
soln[x] ++ ;
}
}
for(auto x : T[u]){
if(x.fi == pp || vis[x.fi]) continue;
dfs1(x.fi, u, ss, l2, x.se);
}
if(ton[u])
upd(l2, +1, true);
}
bool comp(pii a, pii b){
return a.se > b.se;
}
void decomp(int node){
iter ++ ;
dfs(node, -1);
int las = -1;
int sz = sub[node];
bool go = true;
while(go){
go = false;
for(auto x : T[node]){
if(x.fi == las || vis[x.fi]) continue;
if(sub[x.fi] > sz/2){
las = node;
node = x.fi;
go = true;
break;
}
}
}
fron[node]=ton[node]=true;
nani[node] = 0;
sort(T[node].begin(), T[node].end(), comp);
int cv = 0;
for(auto x : T[node]){
if(vis[x.fi]) continue;
cv ++ ;
shd[cv] = x.se;
dfs1(x.fi, node, cv, -1, x.se);
}
for(auto x : qq[node]){
if(dd[node] != dd[x.fi]) continue;
if(ton[x.fi] && in[x.fi] <= x.se){
soln[x.se] = -1;
}
else{
soln[x.se] = -2;
}
}
for(auto x : cc[node]){
soln[x] ++ ; // itself
soln[x] += calc(1, x);
}
for(auto q : up){
upd(q.fi, -q.se, false);
}
up.clear();
vis[node]=true;
for(auto x : T[node]){
if(!vis[x.fi]) decomp(x.fi);
}
}
int main(){
fastIO;
int n, q;
cin >> n >> q;
char typ;
int xx, yy;
bool sol;
for(int iq = 1; iq <= n + q - 1; iq ++ ){
cin >> typ;
if(typ == 'S'){
cin >> xx >> yy;
T[xx].push_back(mp(yy, iq));
T[yy].push_back(mp(xx, iq));
}
else if(typ == 'Q'){
cin >> xx >> yy;
/*
sol = chk(yy, -1, xx, 0);
if(sol == true){
bruh[iq] = -1;
}
else{
bruh[iq] = -2;
}
*/
if(xx == yy)
soln[iq] = -1;
else
qq[yy].push_back(mp(xx, iq));
}
else{
cin >> xx;
cc[xx].push_back(iq);
}
}
lim = n + q;
decomp(1);
for(int i = 1; i <= n + q - 1; i ++ ){
if(soln[i] == 0) continue;
if(soln[i] == -1){
cout << "yes\n";
}
else if(soln[i] == -2){
cout << "no\n";
}
else{
cout << soln[i] << "\n";
}
}
return 0;
}
Compilation message
servers.cpp: In function 'int main()':
servers.cpp:204:10: warning: unused variable 'sol' [-Wunused-variable]
204 | bool sol;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11852 KB |
Output is correct |
2 |
Correct |
48 ms |
12808 KB |
Output is correct |
3 |
Correct |
44 ms |
12744 KB |
Output is correct |
4 |
Correct |
52 ms |
13068 KB |
Output is correct |
5 |
Correct |
53 ms |
12424 KB |
Output is correct |
6 |
Correct |
43 ms |
12544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11852 KB |
Output is correct |
2 |
Correct |
48 ms |
12808 KB |
Output is correct |
3 |
Correct |
44 ms |
12744 KB |
Output is correct |
4 |
Correct |
52 ms |
13068 KB |
Output is correct |
5 |
Correct |
53 ms |
12424 KB |
Output is correct |
6 |
Correct |
43 ms |
12544 KB |
Output is correct |
7 |
Correct |
35 ms |
11852 KB |
Output is correct |
8 |
Correct |
68 ms |
13156 KB |
Output is correct |
9 |
Correct |
56 ms |
13008 KB |
Output is correct |
10 |
Correct |
67 ms |
13252 KB |
Output is correct |
11 |
Correct |
64 ms |
12620 KB |
Output is correct |
12 |
Correct |
50 ms |
12656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
11844 KB |
Output is correct |
2 |
Correct |
231 ms |
23564 KB |
Output is correct |
3 |
Correct |
222 ms |
23232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
11844 KB |
Output is correct |
2 |
Correct |
231 ms |
23564 KB |
Output is correct |
3 |
Correct |
222 ms |
23232 KB |
Output is correct |
4 |
Correct |
42 ms |
12072 KB |
Output is correct |
5 |
Correct |
178 ms |
25252 KB |
Output is correct |
6 |
Correct |
128 ms |
22952 KB |
Output is correct |
7 |
Correct |
137 ms |
23124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11776 KB |
Output is correct |
2 |
Correct |
353 ms |
29632 KB |
Output is correct |
3 |
Correct |
373 ms |
29700 KB |
Output is correct |
4 |
Correct |
247 ms |
31160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11776 KB |
Output is correct |
2 |
Correct |
353 ms |
29632 KB |
Output is correct |
3 |
Correct |
373 ms |
29700 KB |
Output is correct |
4 |
Correct |
247 ms |
31160 KB |
Output is correct |
5 |
Correct |
35 ms |
12180 KB |
Output is correct |
6 |
Correct |
389 ms |
32740 KB |
Output is correct |
7 |
Correct |
334 ms |
33820 KB |
Output is correct |
8 |
Correct |
393 ms |
32324 KB |
Output is correct |
9 |
Correct |
370 ms |
32456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
11844 KB |
Output is correct |
2 |
Correct |
243 ms |
22136 KB |
Output is correct |
3 |
Correct |
283 ms |
21000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
11844 KB |
Output is correct |
2 |
Correct |
243 ms |
22136 KB |
Output is correct |
3 |
Correct |
283 ms |
21000 KB |
Output is correct |
4 |
Correct |
36 ms |
12060 KB |
Output is correct |
5 |
Correct |
339 ms |
24904 KB |
Output is correct |
6 |
Correct |
321 ms |
24056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11852 KB |
Output is correct |
2 |
Correct |
345 ms |
29568 KB |
Output is correct |
3 |
Correct |
346 ms |
29716 KB |
Output is correct |
4 |
Correct |
247 ms |
31132 KB |
Output is correct |
5 |
Correct |
34 ms |
12304 KB |
Output is correct |
6 |
Correct |
248 ms |
22024 KB |
Output is correct |
7 |
Correct |
286 ms |
21080 KB |
Output is correct |
8 |
Correct |
382 ms |
21796 KB |
Output is correct |
9 |
Correct |
371 ms |
22212 KB |
Output is correct |
10 |
Correct |
447 ms |
25924 KB |
Output is correct |
11 |
Correct |
421 ms |
24276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11852 KB |
Output is correct |
2 |
Correct |
345 ms |
29568 KB |
Output is correct |
3 |
Correct |
346 ms |
29716 KB |
Output is correct |
4 |
Correct |
247 ms |
31132 KB |
Output is correct |
5 |
Correct |
34 ms |
12304 KB |
Output is correct |
6 |
Correct |
248 ms |
22024 KB |
Output is correct |
7 |
Correct |
286 ms |
21080 KB |
Output is correct |
8 |
Correct |
382 ms |
21796 KB |
Output is correct |
9 |
Correct |
371 ms |
22212 KB |
Output is correct |
10 |
Correct |
447 ms |
25924 KB |
Output is correct |
11 |
Correct |
421 ms |
24276 KB |
Output is correct |
12 |
Correct |
35 ms |
12084 KB |
Output is correct |
13 |
Correct |
401 ms |
32816 KB |
Output is correct |
14 |
Correct |
336 ms |
33724 KB |
Output is correct |
15 |
Correct |
380 ms |
32572 KB |
Output is correct |
16 |
Correct |
366 ms |
32196 KB |
Output is correct |
17 |
Correct |
36 ms |
12588 KB |
Output is correct |
18 |
Correct |
327 ms |
24892 KB |
Output is correct |
19 |
Correct |
316 ms |
24032 KB |
Output is correct |
20 |
Correct |
399 ms |
24636 KB |
Output is correct |
21 |
Correct |
411 ms |
25328 KB |
Output is correct |
22 |
Correct |
593 ms |
27732 KB |
Output is correct |
23 |
Correct |
591 ms |
28220 KB |
Output is correct |
24 |
Correct |
501 ms |
27520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11844 KB |
Output is correct |
2 |
Correct |
49 ms |
12856 KB |
Output is correct |
3 |
Correct |
45 ms |
12740 KB |
Output is correct |
4 |
Correct |
50 ms |
13092 KB |
Output is correct |
5 |
Correct |
50 ms |
12388 KB |
Output is correct |
6 |
Correct |
43 ms |
12436 KB |
Output is correct |
7 |
Correct |
33 ms |
12384 KB |
Output is correct |
8 |
Correct |
173 ms |
23636 KB |
Output is correct |
9 |
Correct |
176 ms |
23216 KB |
Output is correct |
10 |
Correct |
37 ms |
12236 KB |
Output is correct |
11 |
Correct |
356 ms |
29840 KB |
Output is correct |
12 |
Correct |
341 ms |
29672 KB |
Output is correct |
13 |
Correct |
248 ms |
30776 KB |
Output is correct |
14 |
Correct |
33 ms |
12356 KB |
Output is correct |
15 |
Correct |
243 ms |
21896 KB |
Output is correct |
16 |
Correct |
292 ms |
21040 KB |
Output is correct |
17 |
Correct |
375 ms |
21948 KB |
Output is correct |
18 |
Correct |
375 ms |
22236 KB |
Output is correct |
19 |
Correct |
422 ms |
25820 KB |
Output is correct |
20 |
Correct |
425 ms |
24216 KB |
Output is correct |
21 |
Correct |
199 ms |
22832 KB |
Output is correct |
22 |
Correct |
220 ms |
22504 KB |
Output is correct |
23 |
Correct |
316 ms |
22088 KB |
Output is correct |
24 |
Correct |
317 ms |
22176 KB |
Output is correct |
25 |
Correct |
422 ms |
27596 KB |
Output is correct |
26 |
Correct |
293 ms |
20036 KB |
Output is correct |
27 |
Correct |
284 ms |
19740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
11844 KB |
Output is correct |
2 |
Correct |
49 ms |
12856 KB |
Output is correct |
3 |
Correct |
45 ms |
12740 KB |
Output is correct |
4 |
Correct |
50 ms |
13092 KB |
Output is correct |
5 |
Correct |
50 ms |
12388 KB |
Output is correct |
6 |
Correct |
43 ms |
12436 KB |
Output is correct |
7 |
Correct |
33 ms |
12384 KB |
Output is correct |
8 |
Correct |
173 ms |
23636 KB |
Output is correct |
9 |
Correct |
176 ms |
23216 KB |
Output is correct |
10 |
Correct |
37 ms |
12236 KB |
Output is correct |
11 |
Correct |
356 ms |
29840 KB |
Output is correct |
12 |
Correct |
341 ms |
29672 KB |
Output is correct |
13 |
Correct |
248 ms |
30776 KB |
Output is correct |
14 |
Correct |
33 ms |
12356 KB |
Output is correct |
15 |
Correct |
243 ms |
21896 KB |
Output is correct |
16 |
Correct |
292 ms |
21040 KB |
Output is correct |
17 |
Correct |
375 ms |
21948 KB |
Output is correct |
18 |
Correct |
375 ms |
22236 KB |
Output is correct |
19 |
Correct |
422 ms |
25820 KB |
Output is correct |
20 |
Correct |
425 ms |
24216 KB |
Output is correct |
21 |
Correct |
199 ms |
22832 KB |
Output is correct |
22 |
Correct |
220 ms |
22504 KB |
Output is correct |
23 |
Correct |
316 ms |
22088 KB |
Output is correct |
24 |
Correct |
317 ms |
22176 KB |
Output is correct |
25 |
Correct |
422 ms |
27596 KB |
Output is correct |
26 |
Correct |
293 ms |
20036 KB |
Output is correct |
27 |
Correct |
284 ms |
19740 KB |
Output is correct |
28 |
Correct |
40 ms |
12080 KB |
Output is correct |
29 |
Correct |
66 ms |
13184 KB |
Output is correct |
30 |
Correct |
56 ms |
13020 KB |
Output is correct |
31 |
Correct |
69 ms |
13192 KB |
Output is correct |
32 |
Correct |
65 ms |
12560 KB |
Output is correct |
33 |
Correct |
51 ms |
12600 KB |
Output is correct |
34 |
Correct |
34 ms |
12584 KB |
Output is correct |
35 |
Correct |
177 ms |
25236 KB |
Output is correct |
36 |
Correct |
123 ms |
22872 KB |
Output is correct |
37 |
Correct |
129 ms |
23016 KB |
Output is correct |
38 |
Correct |
36 ms |
12664 KB |
Output is correct |
39 |
Correct |
376 ms |
32816 KB |
Output is correct |
40 |
Correct |
333 ms |
33828 KB |
Output is correct |
41 |
Correct |
379 ms |
32312 KB |
Output is correct |
42 |
Correct |
360 ms |
32200 KB |
Output is correct |
43 |
Correct |
36 ms |
12580 KB |
Output is correct |
44 |
Correct |
338 ms |
24860 KB |
Output is correct |
45 |
Correct |
313 ms |
24004 KB |
Output is correct |
46 |
Correct |
400 ms |
24740 KB |
Output is correct |
47 |
Correct |
407 ms |
25296 KB |
Output is correct |
48 |
Correct |
621 ms |
27732 KB |
Output is correct |
49 |
Correct |
580 ms |
28228 KB |
Output is correct |
50 |
Correct |
496 ms |
27560 KB |
Output is correct |
51 |
Correct |
154 ms |
23984 KB |
Output is correct |
52 |
Correct |
145 ms |
23824 KB |
Output is correct |
53 |
Correct |
135 ms |
23500 KB |
Output is correct |
54 |
Correct |
135 ms |
24012 KB |
Output is correct |
55 |
Correct |
153 ms |
23160 KB |
Output is correct |
56 |
Correct |
315 ms |
23976 KB |
Output is correct |
57 |
Correct |
345 ms |
28312 KB |
Output is correct |
58 |
Correct |
474 ms |
23536 KB |
Output is correct |
59 |
Correct |
295 ms |
22792 KB |
Output is correct |