#include <bits/stdc++.h>
using namespace std;
vector <int> T[202020];
int P[202020], L[202020], N[202020];
int K[202020], C[202020];
int D1[202020], D2[202020], A1[202020], A2[202020];
int n, m, s, mx, mi;
int dfs1(int p, int r, int *D)
{
P[p] = r; D[p] = D[r] + 1; L[p] = 1;
if(D[p] > mx) mx = D[p], mi = p;
for(int &t: T[p]){
if(t != r){
L[p] = max(L[p], dfs1(t, p, D) + 1);
}
}
return L[p];
}
int color(int p, int x, int *D)
{
if(!p || x <= 0) return p;
else if(N[p] == p){
K[C[p]] --; s -= (K[C[p]] == 0);
N[p] = color(P[p], x - 1, D);
}
else N[p] = color(N[p], x - D[p] + D[N[p]], D);
}
void dfs2(int p, int r, int *D, int *A)
{
int m1, m2, v;
s += (K[C[p]] == 0); K[C[p]] ++;
m1 = m2 = v = 0;
for(int &t: T[p]){
if(t != r){
if(L[t] > m1) m2 = m1, m1 = L[t], v = t;
else if(L[t] > m2) m2 = L[t];
}
}
// printf("%d %d %d\n", p, m1, m2);
color(P[p], m2, D);
if(v) dfs2(v, p, D, A);
color(P[p], m1, D);
for(int &t: T[p]){
if(t != r && t != v){
if(N[p] != p){
s += (K[C[p]] == 0); K[C[p]] ++;
N[p] = p;
}
dfs2(t, p, D, A);
}
}
if(N[p] == p){
K[C[p]] --; s -= (K[C[p]] == 0);
N[p] = P[p];
}
A[p] = s;
}
int main()
{
int i, u, v;
scanf("%d%d", &n, &m);
for(i=1; i<n; i++){
scanf("%d%d", &u, &v);
T[u].push_back(v);
T[v].push_back(u);
}
for(i=1; i<=n; i++){
scanf("%d", C + i);
N[i] = i;
}
mx = 0; dfs1(1, 0, D1); u = mi;
mx = 0; dfs1(u, 0, D1); v = mi;
dfs2(u, 0, D1, A1);
// for(i=1; i<=n; i++) printf("%d ", A1[i]); printf("\n");
s = 0;
for(i=1; i<=n; i++){
K[i] = 0; N[i] = i;
}
dfs1(v, 0, D2); dfs2(v, 0, D2, A2);
// for(i=1; i<=n; i++) printf("%d ", A2[i]); printf("\n");
for(i=1; i<=n; i++){
if(D1[i] > D2[i]) printf("%d\n", A1[i]);
else printf("%d\n", A2[i]);
}
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'int color(int, int, int*)':
joi2019_ho_t5.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", C + i);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5248 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
8 ms |
5376 KB |
Output is correct |
5 |
Correct |
8 ms |
5248 KB |
Output is correct |
6 |
Correct |
11 ms |
5500 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Correct |
8 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
7 ms |
5248 KB |
Output is correct |
12 |
Correct |
7 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5376 KB |
Output is correct |
14 |
Correct |
8 ms |
5376 KB |
Output is correct |
15 |
Correct |
7 ms |
5376 KB |
Output is correct |
16 |
Correct |
7 ms |
5248 KB |
Output is correct |
17 |
Correct |
7 ms |
5376 KB |
Output is correct |
18 |
Correct |
7 ms |
5376 KB |
Output is correct |
19 |
Correct |
7 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5504 KB |
Output is correct |
21 |
Correct |
8 ms |
5376 KB |
Output is correct |
22 |
Correct |
7 ms |
5248 KB |
Output is correct |
23 |
Correct |
7 ms |
5248 KB |
Output is correct |
24 |
Correct |
7 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
7 ms |
5248 KB |
Output is correct |
27 |
Correct |
8 ms |
5376 KB |
Output is correct |
28 |
Correct |
7 ms |
5376 KB |
Output is correct |
29 |
Correct |
7 ms |
5376 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
8 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
14700 KB |
Output is correct |
2 |
Correct |
278 ms |
25464 KB |
Output is correct |
3 |
Correct |
36 ms |
8852 KB |
Output is correct |
4 |
Correct |
597 ms |
21980 KB |
Output is correct |
5 |
Correct |
693 ms |
40664 KB |
Output is correct |
6 |
Correct |
632 ms |
31256 KB |
Output is correct |
7 |
Correct |
432 ms |
21876 KB |
Output is correct |
8 |
Correct |
431 ms |
23672 KB |
Output is correct |
9 |
Correct |
470 ms |
23288 KB |
Output is correct |
10 |
Correct |
467 ms |
23216 KB |
Output is correct |
11 |
Correct |
219 ms |
22632 KB |
Output is correct |
12 |
Correct |
537 ms |
33440 KB |
Output is correct |
13 |
Correct |
471 ms |
31016 KB |
Output is correct |
14 |
Correct |
511 ms |
30532 KB |
Output is correct |
15 |
Correct |
199 ms |
22476 KB |
Output is correct |
16 |
Correct |
489 ms |
34568 KB |
Output is correct |
17 |
Correct |
502 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
18936 KB |
Output is correct |
2 |
Correct |
596 ms |
40628 KB |
Output is correct |
3 |
Correct |
46 ms |
9328 KB |
Output is correct |
4 |
Correct |
461 ms |
22652 KB |
Output is correct |
5 |
Correct |
661 ms |
42288 KB |
Output is correct |
6 |
Correct |
631 ms |
32232 KB |
Output is correct |
7 |
Correct |
488 ms |
22844 KB |
Output is correct |
8 |
Correct |
528 ms |
26744 KB |
Output is correct |
9 |
Correct |
568 ms |
25436 KB |
Output is correct |
10 |
Correct |
535 ms |
24328 KB |
Output is correct |
11 |
Correct |
374 ms |
23024 KB |
Output is correct |
12 |
Correct |
669 ms |
38472 KB |
Output is correct |
13 |
Correct |
517 ms |
31120 KB |
Output is correct |
14 |
Correct |
846 ms |
31332 KB |
Output is correct |
15 |
Correct |
203 ms |
23500 KB |
Output is correct |
16 |
Correct |
491 ms |
35952 KB |
Output is correct |
17 |
Correct |
524 ms |
32372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5120 KB |
Output is correct |
2 |
Correct |
8 ms |
5248 KB |
Output is correct |
3 |
Correct |
8 ms |
5248 KB |
Output is correct |
4 |
Correct |
8 ms |
5376 KB |
Output is correct |
5 |
Correct |
8 ms |
5248 KB |
Output is correct |
6 |
Correct |
11 ms |
5500 KB |
Output is correct |
7 |
Correct |
8 ms |
5376 KB |
Output is correct |
8 |
Correct |
8 ms |
5248 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
7 ms |
5248 KB |
Output is correct |
11 |
Correct |
7 ms |
5248 KB |
Output is correct |
12 |
Correct |
7 ms |
5248 KB |
Output is correct |
13 |
Correct |
8 ms |
5376 KB |
Output is correct |
14 |
Correct |
8 ms |
5376 KB |
Output is correct |
15 |
Correct |
7 ms |
5376 KB |
Output is correct |
16 |
Correct |
7 ms |
5248 KB |
Output is correct |
17 |
Correct |
7 ms |
5376 KB |
Output is correct |
18 |
Correct |
7 ms |
5376 KB |
Output is correct |
19 |
Correct |
7 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5504 KB |
Output is correct |
21 |
Correct |
8 ms |
5376 KB |
Output is correct |
22 |
Correct |
7 ms |
5248 KB |
Output is correct |
23 |
Correct |
7 ms |
5248 KB |
Output is correct |
24 |
Correct |
7 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
7 ms |
5248 KB |
Output is correct |
27 |
Correct |
8 ms |
5376 KB |
Output is correct |
28 |
Correct |
7 ms |
5376 KB |
Output is correct |
29 |
Correct |
7 ms |
5376 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
8 ms |
5376 KB |
Output is correct |
33 |
Correct |
167 ms |
14700 KB |
Output is correct |
34 |
Correct |
278 ms |
25464 KB |
Output is correct |
35 |
Correct |
36 ms |
8852 KB |
Output is correct |
36 |
Correct |
597 ms |
21980 KB |
Output is correct |
37 |
Correct |
693 ms |
40664 KB |
Output is correct |
38 |
Correct |
632 ms |
31256 KB |
Output is correct |
39 |
Correct |
432 ms |
21876 KB |
Output is correct |
40 |
Correct |
431 ms |
23672 KB |
Output is correct |
41 |
Correct |
470 ms |
23288 KB |
Output is correct |
42 |
Correct |
467 ms |
23216 KB |
Output is correct |
43 |
Correct |
219 ms |
22632 KB |
Output is correct |
44 |
Correct |
537 ms |
33440 KB |
Output is correct |
45 |
Correct |
471 ms |
31016 KB |
Output is correct |
46 |
Correct |
511 ms |
30532 KB |
Output is correct |
47 |
Correct |
199 ms |
22476 KB |
Output is correct |
48 |
Correct |
489 ms |
34568 KB |
Output is correct |
49 |
Correct |
502 ms |
31324 KB |
Output is correct |
50 |
Correct |
339 ms |
18936 KB |
Output is correct |
51 |
Correct |
596 ms |
40628 KB |
Output is correct |
52 |
Correct |
46 ms |
9328 KB |
Output is correct |
53 |
Correct |
461 ms |
22652 KB |
Output is correct |
54 |
Correct |
661 ms |
42288 KB |
Output is correct |
55 |
Correct |
631 ms |
32232 KB |
Output is correct |
56 |
Correct |
488 ms |
22844 KB |
Output is correct |
57 |
Correct |
528 ms |
26744 KB |
Output is correct |
58 |
Correct |
568 ms |
25436 KB |
Output is correct |
59 |
Correct |
535 ms |
24328 KB |
Output is correct |
60 |
Correct |
374 ms |
23024 KB |
Output is correct |
61 |
Correct |
669 ms |
38472 KB |
Output is correct |
62 |
Correct |
517 ms |
31120 KB |
Output is correct |
63 |
Correct |
846 ms |
31332 KB |
Output is correct |
64 |
Correct |
203 ms |
23500 KB |
Output is correct |
65 |
Correct |
491 ms |
35952 KB |
Output is correct |
66 |
Correct |
524 ms |
32372 KB |
Output is correct |
67 |
Correct |
35 ms |
7544 KB |
Output is correct |
68 |
Correct |
182 ms |
20952 KB |
Output is correct |
69 |
Correct |
303 ms |
21736 KB |
Output is correct |
70 |
Correct |
446 ms |
21856 KB |
Output is correct |
71 |
Correct |
578 ms |
40516 KB |
Output is correct |
72 |
Correct |
564 ms |
31128 KB |
Output is correct |
73 |
Correct |
421 ms |
21884 KB |
Output is correct |
74 |
Correct |
488 ms |
25436 KB |
Output is correct |
75 |
Correct |
472 ms |
23576 KB |
Output is correct |
76 |
Correct |
449 ms |
23352 KB |
Output is correct |
77 |
Correct |
346 ms |
22172 KB |
Output is correct |
78 |
Correct |
565 ms |
35472 KB |
Output is correct |
79 |
Correct |
479 ms |
33256 KB |
Output is correct |
80 |
Correct |
508 ms |
29420 KB |
Output is correct |
81 |
Correct |
184 ms |
22192 KB |
Output is correct |
82 |
Correct |
476 ms |
34504 KB |
Output is correct |
83 |
Correct |
518 ms |
31392 KB |
Output is correct |
84 |
Correct |
486 ms |
22112 KB |
Output is correct |
85 |
Correct |
649 ms |
41328 KB |
Output is correct |
86 |
Correct |
605 ms |
31780 KB |
Output is correct |
87 |
Correct |
469 ms |
22136 KB |
Output is correct |
88 |
Correct |
627 ms |
26240 KB |
Output is correct |
89 |
Correct |
469 ms |
24440 KB |
Output is correct |
90 |
Correct |
478 ms |
23760 KB |
Output is correct |
91 |
Correct |
327 ms |
22640 KB |
Output is correct |
92 |
Correct |
624 ms |
40696 KB |
Output is correct |
93 |
Correct |
467 ms |
28956 KB |
Output is correct |
94 |
Correct |
546 ms |
27072 KB |
Output is correct |
95 |
Correct |
208 ms |
22888 KB |
Output is correct |
96 |
Correct |
535 ms |
35180 KB |
Output is correct |
97 |
Correct |
550 ms |
31820 KB |
Output is correct |
98 |
Correct |
512 ms |
22776 KB |
Output is correct |
99 |
Correct |
654 ms |
41560 KB |
Output is correct |
100 |
Correct |
580 ms |
32220 KB |
Output is correct |
101 |
Correct |
459 ms |
22760 KB |
Output is correct |
102 |
Correct |
499 ms |
25820 KB |
Output is correct |
103 |
Correct |
461 ms |
24624 KB |
Output is correct |
104 |
Correct |
546 ms |
24220 KB |
Output is correct |
105 |
Correct |
337 ms |
22896 KB |
Output is correct |
106 |
Correct |
571 ms |
33656 KB |
Output is correct |
107 |
Correct |
556 ms |
34076 KB |
Output is correct |
108 |
Correct |
587 ms |
28584 KB |
Output is correct |
109 |
Correct |
260 ms |
23404 KB |
Output is correct |
110 |
Correct |
521 ms |
35900 KB |
Output is correct |
111 |
Correct |
583 ms |
32376 KB |
Output is correct |