#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define LN 17 /* LN = ceil(log2(N)) */
#define N_ (N * (LN + 4))
int len[N_], tt[N_][2], ff[N_], fff[N_][2];
int node(int l) {
static int _ = 1;
len[_] = l;
return _++;
}
int ds[N], ans[N], *aa[N], head[N], cnt[N], cnt_[N], ss0[N], ss1[N], pp[N], qq[N];
void append(int i, int a) {
int s;
if (cnt[i] == cnt_[i]) {
int *bb = (int *) malloc((cnt_[i] *= 2) * sizeof *bb), h;
for (h = 0; h < cnt[i]; h++)
bb[h] = aa[i][(head[i] + h) % cnt[i]];
free(aa[i]);
aa[i] = bb, head[i] = 0;
}
aa[i][(head[i] + cnt[i]++) % cnt_[i]] = a;
s = qq[i];
if (len[s] == cnt[i] - 1 || aa[i][(head[i] + cnt[i] - 2 - len[s]) % cnt_[i]] != a)
s = fff[s][a];
if (!tt[s][a]) {
int u = node(len[s] + 2), f = len[u] == 1 ? ss0[i] : tt[fff[s][a]][a];
ff[u] = f;
memcpy(fff[u], fff[f], sizeof fff[f]), fff[u][aa[i][(head[i] + cnt[i] - 1 - len[f]) % cnt_[i]]] = f;
tt[s][a] = u;
ans[i]++;
}
s = tt[s][a];
qq[i] = s;
if (len[s] == cnt[i])
pp[i] = s;
}
void prepend(int i, int a) {
int s;
if (cnt[i] == cnt_[i]) {
int *bb = (int *) malloc((cnt_[i] *= 2) * sizeof *bb), h;
for (h = 0; h < cnt[i]; h++)
bb[h] = aa[i][(head[i] + h) % cnt[i]];
free(aa[i]);
aa[i] = bb, head[i] = 0;
}
head[i] = (head[i] - 1 + cnt_[i]) % cnt_[i], cnt[i]++, aa[i][head[i]] = a;
s = pp[i];
if (len[s] == cnt[i] - 1 || aa[i][(head[i] + 1 + len[s]) % cnt_[i]] != a)
s = fff[s][a];
if (!tt[s][a]) {
int u = node(len[s] + 2), f = len[u] == 1 ? ss0[i] : tt[fff[s][a]][a];
ff[u] = f;
memcpy(fff[u], fff[f], sizeof fff[f]), fff[u][aa[i][(head[i] + len[f]) % cnt_[i]]] = f;
tt[s][a] = u;
ans[i]++;
}
s = tt[s][a];
pp[i] = s;
if (len[s] == cnt[i])
qq[i] = s;
}
int find(int i) {
return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}
void join(int i, int j) {
int h;
i = find(i);
j = find(j);
if (i == j)
return;
if (cnt[i] > cnt[j]) {
ds[j] = i;
for (h = 0; h < cnt[j]; h++)
append(i, aa[j][(head[j] + h) % cnt_[j]]);
free(aa[j]);
} else {
ds[i] = j;
for (h = cnt[i] - 1; h >= 0; h--)
prepend(j, aa[i][(head[i] + h) % cnt_[i]]);
free(aa[i]);
}
}
int main() {
static char cc[N + 1];
int n, h, i, j;
scanf("%d%s", &n, cc);
for (i = 0; i < n; i++) {
aa[i] = (int *) malloc((cnt_[i] = 2) * sizeof *aa[i]);
head[i] = 0;
ss0[i] = node(0), ss1[i] = node(-1);
ff[ss0[i]] = ss1[i];
fff[ss0[i]][0] = fff[ss0[i]][1] = ss1[i], fff[ss1[i]][0] = fff[ss1[i]][1] = ss1[i];
pp[i] = qq[i] = ss0[i];
append(i, cc[i] - '0');
}
memset(ds, -1, n * sizeof *ds);
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
join(i, j);
printf("%d\n", ans[find(i)]);
}
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:106:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d%s", &n, cc);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:118:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
480 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
14732 KB |
Output is correct |
2 |
Correct |
41 ms |
17572 KB |
Output is correct |
3 |
Correct |
38 ms |
15220 KB |
Output is correct |
4 |
Correct |
40 ms |
17776 KB |
Output is correct |
5 |
Correct |
39 ms |
16128 KB |
Output is correct |
6 |
Correct |
38 ms |
17244 KB |
Output is correct |
7 |
Correct |
39 ms |
15908 KB |
Output is correct |
8 |
Correct |
39 ms |
16888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
288 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
292 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
292 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
480 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
556 KB |
Output is correct |
27 |
Correct |
43 ms |
14732 KB |
Output is correct |
28 |
Correct |
41 ms |
17572 KB |
Output is correct |
29 |
Correct |
38 ms |
15220 KB |
Output is correct |
30 |
Correct |
40 ms |
17776 KB |
Output is correct |
31 |
Correct |
39 ms |
16128 KB |
Output is correct |
32 |
Correct |
38 ms |
17244 KB |
Output is correct |
33 |
Correct |
39 ms |
15908 KB |
Output is correct |
34 |
Correct |
39 ms |
16888 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
85 ms |
21704 KB |
Output is correct |
37 |
Correct |
82 ms |
18564 KB |
Output is correct |
38 |
Correct |
83 ms |
22148 KB |
Output is correct |
39 |
Correct |
92 ms |
19728 KB |
Output is correct |
40 |
Correct |
56 ms |
17584 KB |
Output is correct |
41 |
Correct |
55 ms |
16488 KB |
Output is correct |
42 |
Correct |
39 ms |
16908 KB |
Output is correct |
43 |
Correct |
40 ms |
15884 KB |
Output is correct |
44 |
Correct |
38 ms |
16760 KB |
Output is correct |
45 |
Correct |
39 ms |
15792 KB |
Output is correct |
46 |
Correct |
78 ms |
32168 KB |
Output is correct |
47 |
Correct |
69 ms |
20824 KB |
Output is correct |