#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int max(int i, int j) { return i > j ? i : j; }
/*
* For "Q" queries, use LCA + prefix sum on tree ~ 3O(lgn) [for finding lca and almost-lca]
*
* Every B queries of any type, do naive dp on tree (reverse edge order)
* to find answer for "C" queries ~ O(n)
*
* On "C" queries, use cached answer from above and count all edges that appeared after that (by "Q")
* ~ O(B lgn)
*
*
* Time complexity = O(N^2 / B) + O(N lgn) + O(N B lgn)
*
* Let B = sqrt(N / lgN)
*
* Should be able to optimize to O(N^1.5) using https://codeforces.com/blog/entry/71567?mobile=true&locale=en
*/
#define N 120001
#define B 900
int n, q, pp[N], dd[N], up[N], temp, inc[N],
tin[N], tout[N], timer, hld[N], ds[N], sz[N];
int ds_find(int i) {
return ds[i] == i ? i : (ds[i] = ds_find(ds[i]));
}
void pus_(int **eh, int *eo, int i, int j) {
int o = eo[i]++;
if (0 == o) eh[i] = (int*)malloc(2 * sizeof **eh);
else if (0 == (o & o - 1)) eh[i] = (int*)realloc(eh[i], 2 * o * sizeof **eh);
eh[i][o] = j;
}
int *eh[N], *fh[N], eo[N];
void link(int u, int v, int w) {
pus_(eh, eo, u, v);
--eo[u];
pus_(fh, eo, u, w);
pus_(eh, eo, v, u);
--eo[v];
pus_(fh, eo, v, w);
}
void dfs(int u, int p) {
sz[u] = 1;
inc[u] = (up[u] > up[p]) + inc[p];
for (int j = 0; j < eo[u]; ++j) if (eh[u][j] != p) {
int v = eh[u][j];
dd[v] = dd[u] + 1;
pp[v] = u;
up[v] = fh[u][j];
dfs(v, u);
sz[u] += sz[v];
}
for (int j = 0; j < eo[u]; ++j) if (eh[u][j] == p) {
int ww = fh[u][j];
for (int k = j + 1; k < eo[u]; ++k)
eh[u][k - 1] = eh[u][k], fh[u][k - 1] = fh[u][k];
--eo[u];
eh[u][eo[u]] = p;
fh[u][eo[u]] = ww;
break;
}
for (int j = 0; j < eo[u]; ++j) {
int v = eh[u][j];
if (sz[v] > sz[eh[u][0]]) {
temp = eh[u][0], eh[u][0] = v, eh[u][j] = temp;
temp = fh[u][0], fh[u][0] = fh[u][j], fh[u][j] = temp;
}
}
}
void efs(int u, int hd) {
tin[u] = timer++;
hld[u] = hd;
if (eo[u])
efs(eh[u][0], hd);
for (int j = 1; j < eo[u]; ++j)
efs(eh[u][j], eh[u][j]);
tout[u] = timer - 1;
}
int lca(int u, int v) {
while (hld[u] != hld[v]) {
if (dd[hld[u]] >= dd[hld[v]]) u = pp[hld[u]];
else v = pp[hld[v]];
}
return dd[u] <= dd[v] ? u : v;
}
struct Query {
char type[2];
int a, b;
} qq[N * 2];
int navigate(int u, int v) {
int lb = -1, ub = eo[u];
while (ub-lb>1){
int md=lb+(ub-lb)/2;
if(tin[eh[u][md]]<=tin[v])lb=md;
else ub=md;
}
return eh[u][lb];
}
/* u = data node, v = ask node */
int query_yesno(int u, int v, int time) {
if (u == v) return 1;
int a = lca(u, v);
int va, ua;
if (u == a) {
va = navigate(a, v);
return inc[v] - inc[va] == dd[v] - dd[va] && up[v] <= time;
}
if (v == a) {
ua = navigate(a, u);
return inc[u] - inc[ua] == 0 && up[ua] <= time;
}
va = navigate(a, v);
ua = navigate(a, u);
return (dd[v] - dd[va] == inc[v] - inc[va]) && (inc[u] - inc[ua] == 0)
&& up[va] > up[ua] && up[v] <= time;
}
int dp[N];
void build(int ii, int tme) {
for (int i = 1; i <= n; ++i)
dp[i] = 1;
for (int i = 1; i <= n; ++i)
ds_find(i);
for (int i = ii; i >= 0; --i) {
if (qq[i].type[0] != 'S')
continue;
int x = dp[qq[i].a], y = dp[qq[i].b];
dp[qq[i].a] += y;
dp[qq[i].b] += x;
}
}
int counted[N];
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i)
ds[i] = i;
q = n + q - 1;
for (int j = 0, i = 0; i < q; ++i) {
int a, b = 0;
char *s = qq[i].type;
scanf(" %s%d", s, &a);
if (s[0] == 'S') {
scanf("%d", &b);
link(a, b, j++);
} else if (s[0] == 'Q')
scanf("%d", &b);
qq[i].a = a, qq[i].b = b;
}
up[1] = 1e9;
pp[1] = 1;
dfs(1, 1);
efs(1, 1);
build(-1, -1);
int at = 0;
for (int j = 0, i = 0; i < q; ++i) {
if (qq[i].type[0] == 'S')
++j;
if (i % B == 0) {
for (int k = at; k <= i; ++k)
if (qq[k].type[0] == 'S')
ds[ds_find(qq[k].a)] = qq[k].b;
build(i, j - 1);
at = i + 1;
}
if (qq[i].type[0] == 'Q')
puts(query_yesno(qq[i].b, qq[i].a, j - 1) ? "yes": "no");
else if (qq[i].type[0] == 'C') {
int focus = qq[i].a;
int addition = 0;
for (int k = at; k < i; ++k)
if (qq[k].type[0] == 'S') {
int a = qq[k].a, b = qq[k].b;
if (counted[a] != i + 1 &&
ds[a] != ds[focus] && query_yesno(focus, a, j - 1))
++addition, counted[a] = i + 1;
if (counted[b] != i + 1 &&
ds[b] != ds[focus] && query_yesno(focus, b, j - 1))
++addition, counted[b] = i + 1;
}
printf("%d\n", dp[focus] + addition);
}
}
}
Compilation message
servers.c: In function 'pus_':
servers.c:36:26: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
36 | else if (0 == (o & o - 1)) eh[i] = (int*)realloc(eh[i], 2 * o * sizeof **eh);
| ~~^~~
servers.c: In function 'main':
servers.c:157:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
157 | scanf("%d%d", &n, &q);
| ^~~~~~~~~~~~~~~~~~~~~
servers.c:166:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
166 | scanf(" %s%d", s, &a);
| ^~~~~~~~~~~~~~~~~~~~~
servers.c:168:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | scanf("%d", &b);
| ^~~~~~~~~~~~~~~
servers.c:171:13: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
171 | scanf("%d", &b);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
38 ms |
7508 KB |
Output is correct |
3 |
Correct |
34 ms |
7644 KB |
Output is correct |
4 |
Correct |
30 ms |
7516 KB |
Output is correct |
5 |
Correct |
27 ms |
7772 KB |
Output is correct |
6 |
Correct |
37 ms |
7448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
38 ms |
7508 KB |
Output is correct |
3 |
Correct |
34 ms |
7644 KB |
Output is correct |
4 |
Correct |
30 ms |
7516 KB |
Output is correct |
5 |
Correct |
27 ms |
7772 KB |
Output is correct |
6 |
Correct |
37 ms |
7448 KB |
Output is correct |
7 |
Correct |
22 ms |
7004 KB |
Output is correct |
8 |
Correct |
108 ms |
7332 KB |
Output is correct |
9 |
Correct |
78 ms |
7504 KB |
Output is correct |
10 |
Correct |
79 ms |
7436 KB |
Output is correct |
11 |
Correct |
40 ms |
7764 KB |
Output is correct |
12 |
Correct |
122 ms |
7668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6992 KB |
Output is correct |
2 |
Correct |
210 ms |
19084 KB |
Output is correct |
3 |
Correct |
214 ms |
19052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
6992 KB |
Output is correct |
2 |
Correct |
210 ms |
19084 KB |
Output is correct |
3 |
Correct |
214 ms |
19052 KB |
Output is correct |
4 |
Correct |
24 ms |
7096 KB |
Output is correct |
5 |
Correct |
1099 ms |
19464 KB |
Output is correct |
6 |
Correct |
2154 ms |
19908 KB |
Output is correct |
7 |
Execution timed out |
3530 ms |
19544 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
196 ms |
29264 KB |
Output is correct |
3 |
Correct |
170 ms |
29388 KB |
Output is correct |
4 |
Correct |
106 ms |
29428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
196 ms |
29264 KB |
Output is correct |
3 |
Correct |
170 ms |
29388 KB |
Output is correct |
4 |
Correct |
106 ms |
29428 KB |
Output is correct |
5 |
Correct |
29 ms |
7056 KB |
Output is correct |
6 |
Correct |
456 ms |
29780 KB |
Output is correct |
7 |
Correct |
273 ms |
29780 KB |
Output is correct |
8 |
Correct |
654 ms |
29520 KB |
Output is correct |
9 |
Correct |
680 ms |
32324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
119 ms |
18004 KB |
Output is correct |
3 |
Correct |
197 ms |
18096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
119 ms |
18004 KB |
Output is correct |
3 |
Correct |
197 ms |
18096 KB |
Output is correct |
4 |
Correct |
25 ms |
7004 KB |
Output is correct |
5 |
Correct |
882 ms |
18432 KB |
Output is correct |
6 |
Correct |
1770 ms |
18768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
181 ms |
29372 KB |
Output is correct |
3 |
Correct |
200 ms |
29460 KB |
Output is correct |
4 |
Correct |
104 ms |
29268 KB |
Output is correct |
5 |
Correct |
20 ms |
7000 KB |
Output is correct |
6 |
Correct |
120 ms |
18004 KB |
Output is correct |
7 |
Correct |
220 ms |
18000 KB |
Output is correct |
8 |
Correct |
179 ms |
19124 KB |
Output is correct |
9 |
Correct |
190 ms |
19024 KB |
Output is correct |
10 |
Correct |
189 ms |
22192 KB |
Output is correct |
11 |
Correct |
168 ms |
21420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
181 ms |
29372 KB |
Output is correct |
3 |
Correct |
200 ms |
29460 KB |
Output is correct |
4 |
Correct |
104 ms |
29268 KB |
Output is correct |
5 |
Correct |
20 ms |
7000 KB |
Output is correct |
6 |
Correct |
120 ms |
18004 KB |
Output is correct |
7 |
Correct |
220 ms |
18000 KB |
Output is correct |
8 |
Correct |
179 ms |
19124 KB |
Output is correct |
9 |
Correct |
190 ms |
19024 KB |
Output is correct |
10 |
Correct |
189 ms |
22192 KB |
Output is correct |
11 |
Correct |
168 ms |
21420 KB |
Output is correct |
12 |
Correct |
20 ms |
7052 KB |
Output is correct |
13 |
Correct |
461 ms |
30036 KB |
Output is correct |
14 |
Correct |
272 ms |
29776 KB |
Output is correct |
15 |
Correct |
684 ms |
29516 KB |
Output is correct |
16 |
Correct |
713 ms |
32340 KB |
Output is correct |
17 |
Correct |
22 ms |
8024 KB |
Output is correct |
18 |
Correct |
866 ms |
21332 KB |
Output is correct |
19 |
Correct |
1842 ms |
21332 KB |
Output is correct |
20 |
Correct |
492 ms |
22296 KB |
Output is correct |
21 |
Correct |
1598 ms |
22436 KB |
Output is correct |
22 |
Correct |
982 ms |
24344 KB |
Output is correct |
23 |
Correct |
382 ms |
26240 KB |
Output is correct |
24 |
Correct |
229 ms |
26452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
34 ms |
7304 KB |
Output is correct |
3 |
Correct |
41 ms |
7628 KB |
Output is correct |
4 |
Correct |
29 ms |
7468 KB |
Output is correct |
5 |
Correct |
24 ms |
7768 KB |
Output is correct |
6 |
Correct |
35 ms |
7516 KB |
Output is correct |
7 |
Correct |
22 ms |
7004 KB |
Output is correct |
8 |
Correct |
217 ms |
19244 KB |
Output is correct |
9 |
Correct |
209 ms |
19024 KB |
Output is correct |
10 |
Correct |
19 ms |
7256 KB |
Output is correct |
11 |
Correct |
191 ms |
29344 KB |
Output is correct |
12 |
Correct |
191 ms |
29264 KB |
Output is correct |
13 |
Correct |
102 ms |
29268 KB |
Output is correct |
14 |
Correct |
24 ms |
7116 KB |
Output is correct |
15 |
Correct |
121 ms |
18176 KB |
Output is correct |
16 |
Correct |
215 ms |
18088 KB |
Output is correct |
17 |
Correct |
216 ms |
19028 KB |
Output is correct |
18 |
Correct |
201 ms |
19032 KB |
Output is correct |
19 |
Correct |
191 ms |
22352 KB |
Output is correct |
20 |
Correct |
192 ms |
21328 KB |
Output is correct |
21 |
Correct |
205 ms |
19036 KB |
Output is correct |
22 |
Correct |
205 ms |
19024 KB |
Output is correct |
23 |
Correct |
207 ms |
19332 KB |
Output is correct |
24 |
Correct |
212 ms |
19456 KB |
Output is correct |
25 |
Correct |
281 ms |
21824 KB |
Output is correct |
26 |
Correct |
184 ms |
18348 KB |
Output is correct |
27 |
Correct |
178 ms |
18516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7004 KB |
Output is correct |
2 |
Correct |
34 ms |
7304 KB |
Output is correct |
3 |
Correct |
41 ms |
7628 KB |
Output is correct |
4 |
Correct |
29 ms |
7468 KB |
Output is correct |
5 |
Correct |
24 ms |
7768 KB |
Output is correct |
6 |
Correct |
35 ms |
7516 KB |
Output is correct |
7 |
Correct |
22 ms |
7004 KB |
Output is correct |
8 |
Correct |
217 ms |
19244 KB |
Output is correct |
9 |
Correct |
209 ms |
19024 KB |
Output is correct |
10 |
Correct |
19 ms |
7256 KB |
Output is correct |
11 |
Correct |
191 ms |
29344 KB |
Output is correct |
12 |
Correct |
191 ms |
29264 KB |
Output is correct |
13 |
Correct |
102 ms |
29268 KB |
Output is correct |
14 |
Correct |
24 ms |
7116 KB |
Output is correct |
15 |
Correct |
121 ms |
18176 KB |
Output is correct |
16 |
Correct |
215 ms |
18088 KB |
Output is correct |
17 |
Correct |
216 ms |
19028 KB |
Output is correct |
18 |
Correct |
201 ms |
19032 KB |
Output is correct |
19 |
Correct |
191 ms |
22352 KB |
Output is correct |
20 |
Correct |
192 ms |
21328 KB |
Output is correct |
21 |
Correct |
205 ms |
19036 KB |
Output is correct |
22 |
Correct |
205 ms |
19024 KB |
Output is correct |
23 |
Correct |
207 ms |
19332 KB |
Output is correct |
24 |
Correct |
212 ms |
19456 KB |
Output is correct |
25 |
Correct |
281 ms |
21824 KB |
Output is correct |
26 |
Correct |
184 ms |
18348 KB |
Output is correct |
27 |
Correct |
178 ms |
18516 KB |
Output is correct |
28 |
Correct |
22 ms |
7004 KB |
Output is correct |
29 |
Correct |
106 ms |
7340 KB |
Output is correct |
30 |
Correct |
75 ms |
7504 KB |
Output is correct |
31 |
Correct |
65 ms |
7504 KB |
Output is correct |
32 |
Correct |
51 ms |
7756 KB |
Output is correct |
33 |
Correct |
125 ms |
7540 KB |
Output is correct |
34 |
Correct |
25 ms |
7004 KB |
Output is correct |
35 |
Correct |
1201 ms |
19588 KB |
Output is correct |
36 |
Correct |
2249 ms |
19716 KB |
Output is correct |
37 |
Execution timed out |
3551 ms |
19524 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |