#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>edge[212121], L[2][212121]; int color[212121];
int depth[2][212121], D[2][212121], nxtD[2][212121], mx, mxw;
void dfs(int fl, int w, int bef, int dep) {
depth[fl][w] = dep; D[fl][w] = 0;
if (mx < dep)mx = dep, mxw = w;
for (int nxt : edge[w])if (nxt != bef) {
dfs(fl, nxt, w, dep + 1);
if (D[fl][w] < D[fl][nxt] + 1)nxtD[fl][w] = nxt;
D[fl][w] = max(D[fl][w], D[fl][nxt] + 1);
}
}
int P[2][212121], ans[2][212121], cnt;
int colorCnt, cN[212121];
void coloring(int color, int cnt) {
int befC = !!cN[color]; cN[color] += cnt;
if (befC != !!cN[color]) colorCnt += cnt;
}
int eraseC[212121];
void prColor(int fl, int w, int len, int cnt) {
int now = w;
for (int i = 0; i < len; i++) {
if (now < 0)break;
int befC = !!eraseC[now]; eraseC[now] += cnt;
if (befC != !!eraseC[now])coloring(color[now], -cnt);
now = P[fl][now];
}
}
void get_ans(int fl, int w, int bef, int is_mxline) {
for (int g : L[fl][w]) if (g > 0) prColor(fl, g, 1, -1);
P[fl][w] = bef;
if (bef > 0 && edge[w].size() == 1) {
ans[fl][w] = colorCnt;
for (int g : L[fl][w]) if (g > 0) prColor(fl, g, 1, 1);
return;
}
if (!is_mxline) {
prColor(fl, P[fl][w], D[fl][w], 1);
int now = w, len = D[fl][w];
vector<int>z, x, y;
for (int i = 0; i < len; i++) { if (now > 0)now = P[fl][now]; z.push_back(now); }
for (int i = len - 1; i >= 0; i--)x.push_back(z[i]);
x.push_back(w); now = w;
for (int i = 0; i < len; i++) { now = nxtD[fl][now]; x.push_back(now); y.push_back(now); }
for (int i = 0, j = 0; i < len; i++, j += 2) {
int a = y[i], b = x[j], c = x[j + 1];
L[fl][a].push_back(b); L[fl][a].push_back(c);
}
}
ans[fl][w] = colorCnt;
coloring(color[w], 1);
int mx = 0;
for (int nxt : edge[w]) if (nxt != bef && nxt != nxtD[fl][w]) {
get_ans(fl, nxt, w, 0);
if (mx < D[fl][nxt] + 1) mx = D[fl][nxt] + 1;
}
prColor(fl, w, 1, 1);
prColor(fl, P[fl][w], mx, 1);
get_ans(fl, nxtD[fl][w], w, 1);
prColor(fl, P[fl][w], mx, -1);
if (!is_mxline) {
prColor(fl, P[fl][w], D[fl][w], -1);
}
prColor(fl, w, 1, -1); coloring(color[w], -1);
for (int g : L[fl][w]) if (g > 0) prColor(fl, g, 1, 1);
}
int main() {
int n, m, i, j; scanf("%d%d", &n, &m);
if (n == 1) { printf("0"); return 0; }
for (i = 0; i < n - 1; i++) {
int s, e; scanf("%d%d", &s, &e);
edge[s].push_back(e); edge[e].push_back(s);
}
for (i = 1; i <= n; i++)scanf("%d", &color[i]);
int l, r;
mx = mxw = -1; dfs(0, 1, -1, 0); l = mxw;
mx = mxw = -1; dfs(0, l, -1, 0); r = mxw;
dfs(1, r, -1, 0);
get_ans(1, r, -1, 0);
get_ans(0, l, -1, 0);
for (i = 1; i <= n; i++) {
if (depth[0][i] < depth[1][i]) printf("%d\n", ans[1][i]);
else printf("%d\n", ans[0][i]);
}
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:74:15: warning: unused variable 'j' [-Wunused-variable]
int n, m, i, j; scanf("%d%d", &n, &m);
^
joi2019_ho_t5.cpp:74:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, m, i, j; scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:77:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int s, e; scanf("%d%d", &s, &e);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:80:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 1; i <= n; i++)scanf("%d", &color[i]);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15224 KB |
Output is correct |
2 |
Correct |
15 ms |
15480 KB |
Output is correct |
3 |
Correct |
14 ms |
15608 KB |
Output is correct |
4 |
Correct |
15 ms |
15736 KB |
Output is correct |
5 |
Correct |
15 ms |
15608 KB |
Output is correct |
6 |
Correct |
15 ms |
16120 KB |
Output is correct |
7 |
Correct |
16 ms |
15864 KB |
Output is correct |
8 |
Correct |
17 ms |
15608 KB |
Output is correct |
9 |
Correct |
17 ms |
15608 KB |
Output is correct |
10 |
Correct |
18 ms |
15608 KB |
Output is correct |
11 |
Correct |
16 ms |
15608 KB |
Output is correct |
12 |
Correct |
15 ms |
15480 KB |
Output is correct |
13 |
Correct |
17 ms |
15992 KB |
Output is correct |
14 |
Correct |
17 ms |
15736 KB |
Output is correct |
15 |
Correct |
21 ms |
15736 KB |
Output is correct |
16 |
Correct |
15 ms |
15480 KB |
Output is correct |
17 |
Correct |
16 ms |
15864 KB |
Output is correct |
18 |
Correct |
16 ms |
15736 KB |
Output is correct |
19 |
Correct |
18 ms |
15608 KB |
Output is correct |
20 |
Correct |
16 ms |
16120 KB |
Output is correct |
21 |
Correct |
17 ms |
15864 KB |
Output is correct |
22 |
Correct |
16 ms |
15608 KB |
Output is correct |
23 |
Correct |
16 ms |
15608 KB |
Output is correct |
24 |
Correct |
18 ms |
15736 KB |
Output is correct |
25 |
Correct |
16 ms |
15612 KB |
Output is correct |
26 |
Correct |
16 ms |
15480 KB |
Output is correct |
27 |
Correct |
16 ms |
15992 KB |
Output is correct |
28 |
Correct |
16 ms |
15864 KB |
Output is correct |
29 |
Correct |
16 ms |
15736 KB |
Output is correct |
30 |
Correct |
15 ms |
15608 KB |
Output is correct |
31 |
Correct |
16 ms |
15992 KB |
Output is correct |
32 |
Correct |
16 ms |
15864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
262 ms |
29048 KB |
Output is correct |
2 |
Correct |
313 ms |
59244 KB |
Output is correct |
3 |
Correct |
60 ms |
22652 KB |
Output is correct |
4 |
Correct |
552 ms |
42232 KB |
Output is correct |
5 |
Correct |
632 ms |
94120 KB |
Output is correct |
6 |
Correct |
637 ms |
68468 KB |
Output is correct |
7 |
Correct |
509 ms |
41284 KB |
Output is correct |
8 |
Correct |
546 ms |
46320 KB |
Output is correct |
9 |
Correct |
533 ms |
44900 KB |
Output is correct |
10 |
Correct |
522 ms |
44676 KB |
Output is correct |
11 |
Correct |
225 ms |
35944 KB |
Output is correct |
12 |
Correct |
590 ms |
72172 KB |
Output is correct |
13 |
Correct |
500 ms |
64376 KB |
Output is correct |
14 |
Correct |
555 ms |
64308 KB |
Output is correct |
15 |
Correct |
194 ms |
32616 KB |
Output is correct |
16 |
Correct |
494 ms |
74812 KB |
Output is correct |
17 |
Correct |
565 ms |
66768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
34876 KB |
Output is correct |
2 |
Correct |
656 ms |
89832 KB |
Output is correct |
3 |
Correct |
69 ms |
24044 KB |
Output is correct |
4 |
Correct |
562 ms |
43772 KB |
Output is correct |
5 |
Correct |
670 ms |
96680 KB |
Output is correct |
6 |
Correct |
696 ms |
70464 KB |
Output is correct |
7 |
Correct |
531 ms |
43084 KB |
Output is correct |
8 |
Correct |
586 ms |
52404 KB |
Output is correct |
9 |
Correct |
587 ms |
49236 KB |
Output is correct |
10 |
Correct |
572 ms |
46836 KB |
Output is correct |
11 |
Correct |
421 ms |
41200 KB |
Output is correct |
12 |
Correct |
631 ms |
84852 KB |
Output is correct |
13 |
Correct |
546 ms |
62900 KB |
Output is correct |
14 |
Correct |
595 ms |
65248 KB |
Output is correct |
15 |
Correct |
214 ms |
33896 KB |
Output is correct |
16 |
Correct |
536 ms |
76988 KB |
Output is correct |
17 |
Correct |
596 ms |
68812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
15224 KB |
Output is correct |
2 |
Correct |
15 ms |
15480 KB |
Output is correct |
3 |
Correct |
14 ms |
15608 KB |
Output is correct |
4 |
Correct |
15 ms |
15736 KB |
Output is correct |
5 |
Correct |
15 ms |
15608 KB |
Output is correct |
6 |
Correct |
15 ms |
16120 KB |
Output is correct |
7 |
Correct |
16 ms |
15864 KB |
Output is correct |
8 |
Correct |
17 ms |
15608 KB |
Output is correct |
9 |
Correct |
17 ms |
15608 KB |
Output is correct |
10 |
Correct |
18 ms |
15608 KB |
Output is correct |
11 |
Correct |
16 ms |
15608 KB |
Output is correct |
12 |
Correct |
15 ms |
15480 KB |
Output is correct |
13 |
Correct |
17 ms |
15992 KB |
Output is correct |
14 |
Correct |
17 ms |
15736 KB |
Output is correct |
15 |
Correct |
21 ms |
15736 KB |
Output is correct |
16 |
Correct |
15 ms |
15480 KB |
Output is correct |
17 |
Correct |
16 ms |
15864 KB |
Output is correct |
18 |
Correct |
16 ms |
15736 KB |
Output is correct |
19 |
Correct |
18 ms |
15608 KB |
Output is correct |
20 |
Correct |
16 ms |
16120 KB |
Output is correct |
21 |
Correct |
17 ms |
15864 KB |
Output is correct |
22 |
Correct |
16 ms |
15608 KB |
Output is correct |
23 |
Correct |
16 ms |
15608 KB |
Output is correct |
24 |
Correct |
18 ms |
15736 KB |
Output is correct |
25 |
Correct |
16 ms |
15612 KB |
Output is correct |
26 |
Correct |
16 ms |
15480 KB |
Output is correct |
27 |
Correct |
16 ms |
15992 KB |
Output is correct |
28 |
Correct |
16 ms |
15864 KB |
Output is correct |
29 |
Correct |
16 ms |
15736 KB |
Output is correct |
30 |
Correct |
15 ms |
15608 KB |
Output is correct |
31 |
Correct |
16 ms |
15992 KB |
Output is correct |
32 |
Correct |
16 ms |
15864 KB |
Output is correct |
33 |
Correct |
262 ms |
29048 KB |
Output is correct |
34 |
Correct |
313 ms |
59244 KB |
Output is correct |
35 |
Correct |
60 ms |
22652 KB |
Output is correct |
36 |
Correct |
552 ms |
42232 KB |
Output is correct |
37 |
Correct |
632 ms |
94120 KB |
Output is correct |
38 |
Correct |
637 ms |
68468 KB |
Output is correct |
39 |
Correct |
509 ms |
41284 KB |
Output is correct |
40 |
Correct |
546 ms |
46320 KB |
Output is correct |
41 |
Correct |
533 ms |
44900 KB |
Output is correct |
42 |
Correct |
522 ms |
44676 KB |
Output is correct |
43 |
Correct |
225 ms |
35944 KB |
Output is correct |
44 |
Correct |
590 ms |
72172 KB |
Output is correct |
45 |
Correct |
500 ms |
64376 KB |
Output is correct |
46 |
Correct |
555 ms |
64308 KB |
Output is correct |
47 |
Correct |
194 ms |
32616 KB |
Output is correct |
48 |
Correct |
494 ms |
74812 KB |
Output is correct |
49 |
Correct |
565 ms |
66768 KB |
Output is correct |
50 |
Correct |
411 ms |
34876 KB |
Output is correct |
51 |
Correct |
656 ms |
89832 KB |
Output is correct |
52 |
Correct |
69 ms |
24044 KB |
Output is correct |
53 |
Correct |
562 ms |
43772 KB |
Output is correct |
54 |
Correct |
670 ms |
96680 KB |
Output is correct |
55 |
Correct |
696 ms |
70464 KB |
Output is correct |
56 |
Correct |
531 ms |
43084 KB |
Output is correct |
57 |
Correct |
586 ms |
52404 KB |
Output is correct |
58 |
Correct |
587 ms |
49236 KB |
Output is correct |
59 |
Correct |
572 ms |
46836 KB |
Output is correct |
60 |
Correct |
421 ms |
41200 KB |
Output is correct |
61 |
Correct |
631 ms |
84852 KB |
Output is correct |
62 |
Correct |
546 ms |
62900 KB |
Output is correct |
63 |
Correct |
595 ms |
65248 KB |
Output is correct |
64 |
Correct |
214 ms |
33896 KB |
Output is correct |
65 |
Correct |
536 ms |
76988 KB |
Output is correct |
66 |
Correct |
596 ms |
68812 KB |
Output is correct |
67 |
Correct |
53 ms |
19064 KB |
Output is correct |
68 |
Correct |
234 ms |
50148 KB |
Output is correct |
69 |
Correct |
357 ms |
48260 KB |
Output is correct |
70 |
Correct |
544 ms |
42104 KB |
Output is correct |
71 |
Correct |
673 ms |
94244 KB |
Output is correct |
72 |
Correct |
674 ms |
67784 KB |
Output is correct |
73 |
Correct |
530 ms |
41464 KB |
Output is correct |
74 |
Correct |
572 ms |
50448 KB |
Output is correct |
75 |
Correct |
570 ms |
45848 KB |
Output is correct |
76 |
Correct |
568 ms |
45172 KB |
Output is correct |
77 |
Correct |
404 ms |
38900 KB |
Output is correct |
78 |
Correct |
619 ms |
78872 KB |
Output is correct |
79 |
Correct |
522 ms |
70508 KB |
Output is correct |
80 |
Correct |
574 ms |
61240 KB |
Output is correct |
81 |
Correct |
204 ms |
32632 KB |
Output is correct |
82 |
Correct |
520 ms |
74684 KB |
Output is correct |
83 |
Correct |
564 ms |
66768 KB |
Output is correct |
84 |
Correct |
554 ms |
42232 KB |
Output is correct |
85 |
Correct |
683 ms |
94828 KB |
Output is correct |
86 |
Correct |
669 ms |
68156 KB |
Output is correct |
87 |
Correct |
537 ms |
41976 KB |
Output is correct |
88 |
Correct |
577 ms |
51344 KB |
Output is correct |
89 |
Correct |
580 ms |
46840 KB |
Output is correct |
90 |
Correct |
557 ms |
45588 KB |
Output is correct |
91 |
Correct |
419 ms |
39400 KB |
Output is correct |
92 |
Correct |
657 ms |
92756 KB |
Output is correct |
93 |
Correct |
547 ms |
57044 KB |
Output is correct |
94 |
Correct |
569 ms |
53532 KB |
Output is correct |
95 |
Correct |
204 ms |
33004 KB |
Output is correct |
96 |
Correct |
534 ms |
75460 KB |
Output is correct |
97 |
Correct |
572 ms |
67536 KB |
Output is correct |
98 |
Correct |
571 ms |
43704 KB |
Output is correct |
99 |
Correct |
684 ms |
95272 KB |
Output is correct |
100 |
Correct |
697 ms |
70136 KB |
Output is correct |
101 |
Correct |
531 ms |
42100 KB |
Output is correct |
102 |
Correct |
581 ms |
49908 KB |
Output is correct |
103 |
Correct |
587 ms |
46988 KB |
Output is correct |
104 |
Correct |
565 ms |
46064 KB |
Output is correct |
105 |
Correct |
389 ms |
39408 KB |
Output is correct |
106 |
Correct |
620 ms |
69972 KB |
Output is correct |
107 |
Correct |
512 ms |
70428 KB |
Output is correct |
108 |
Correct |
584 ms |
57048 KB |
Output is correct |
109 |
Correct |
218 ms |
33640 KB |
Output is correct |
110 |
Correct |
534 ms |
76084 KB |
Output is correct |
111 |
Correct |
581 ms |
68432 KB |
Output is correct |