#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
int n, m;
vector<int> link[200010];
int col[200010], d[200010], h[200010], ans[200010];
void dfs1(int num, int par){
d[num]=d[par]+1, h[num]=1;
for(auto i:link[num]){
if(i==par)continue;
dfs1(i, num);
h[num]=max(h[num], h[i]+1);
}
}
int stk[200010], re, cnt[200010], anss;
inline void update(int col, int num){
if(!cnt[col])anss++;
cnt[col]+=num;
if(!cnt[col])anss--;
}
void dfs2(int num, int par){
int max1=0, max2=0, maxnum=0;
for(auto i:link[num]){
if(i==par)continue;
if(max2<=h[i]){
if(max1<=h[i]){
max2=max1;
max1=h[i];
maxnum=i;
}
else max2=h[i];
}
}
while(re&&d[num]-d[stk[re]]<=max2)update(col[stk[re--]], -1);
if(maxnum){
update(col[num], 1);
stk[++re]=num;
dfs2(maxnum, num);
}
while(re&&d[num]-d[stk[re]]<=max1)update(col[stk[re--]], -1);
ans[num]=max(ans[num], anss);
for(auto i:link[num]){
if(i==par||i==maxnum)continue;
if(stk[re]!=num){
update(col[num], 1);
stk[++re]=num;
}
dfs2(i, num);
}
if(stk[re]==num){
update(col[num], -1);
re--;
}
}
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<n; i++){
int a, b;
scanf("%d %d", &a, &b);
link[a].eb(b);
link[b].eb(a);
}
for(int i=1; i<=n; i++)scanf("%d", &col[i]);
dfs1(1, 0);
int rt1=max_element(d+1, d+n+1)-d;
dfs1(rt1, 0);
dfs2(rt1, 0);
int rt2=max_element(d+1, d+n+1)-d;
dfs1(rt2, 0);
dfs2(rt2, 0);
for(int i=1; i<=n; i++)printf("%d\n", ans[i]);
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:56:10: 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:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:63:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=n; i++)scanf("%d", &col[i]);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
11 ms |
5120 KB |
Output is correct |
3 |
Correct |
9 ms |
5120 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
12 ms |
5248 KB |
Output is correct |
6 |
Correct |
13 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5248 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
9 ms |
5120 KB |
Output is correct |
10 |
Correct |
11 ms |
5120 KB |
Output is correct |
11 |
Correct |
9 ms |
5248 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
10 ms |
5248 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
9 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5248 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
10 ms |
5248 KB |
Output is correct |
20 |
Correct |
10 ms |
5376 KB |
Output is correct |
21 |
Correct |
9 ms |
5224 KB |
Output is correct |
22 |
Correct |
9 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5248 KB |
Output is correct |
24 |
Correct |
9 ms |
5248 KB |
Output is correct |
25 |
Correct |
11 ms |
5224 KB |
Output is correct |
26 |
Correct |
11 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
5376 KB |
Output is correct |
28 |
Correct |
9 ms |
5248 KB |
Output is correct |
29 |
Correct |
10 ms |
5248 KB |
Output is correct |
30 |
Correct |
9 ms |
5248 KB |
Output is correct |
31 |
Correct |
9 ms |
5376 KB |
Output is correct |
32 |
Correct |
9 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
12408 KB |
Output is correct |
2 |
Correct |
221 ms |
21752 KB |
Output is correct |
3 |
Correct |
37 ms |
7936 KB |
Output is correct |
4 |
Correct |
422 ms |
17860 KB |
Output is correct |
5 |
Correct |
459 ms |
34316 KB |
Output is correct |
6 |
Correct |
522 ms |
25848 KB |
Output is correct |
7 |
Correct |
342 ms |
17908 KB |
Output is correct |
8 |
Correct |
434 ms |
19576 KB |
Output is correct |
9 |
Correct |
388 ms |
19048 KB |
Output is correct |
10 |
Correct |
359 ms |
18936 KB |
Output is correct |
11 |
Correct |
207 ms |
18648 KB |
Output is correct |
12 |
Correct |
436 ms |
28024 KB |
Output is correct |
13 |
Correct |
379 ms |
25844 KB |
Output is correct |
14 |
Correct |
389 ms |
25364 KB |
Output is correct |
15 |
Correct |
195 ms |
18540 KB |
Output is correct |
16 |
Correct |
367 ms |
29040 KB |
Output is correct |
17 |
Correct |
444 ms |
26228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
16504 KB |
Output is correct |
2 |
Correct |
480 ms |
35320 KB |
Output is correct |
3 |
Correct |
39 ms |
8568 KB |
Output is correct |
4 |
Correct |
386 ms |
19448 KB |
Output is correct |
5 |
Correct |
477 ms |
36728 KB |
Output is correct |
6 |
Correct |
490 ms |
27768 KB |
Output is correct |
7 |
Correct |
374 ms |
19576 KB |
Output is correct |
8 |
Correct |
474 ms |
23136 KB |
Output is correct |
9 |
Correct |
399 ms |
21880 KB |
Output is correct |
10 |
Correct |
460 ms |
20856 KB |
Output is correct |
11 |
Correct |
305 ms |
20080 KB |
Output is correct |
12 |
Correct |
458 ms |
33144 KB |
Output is correct |
13 |
Correct |
389 ms |
26740 KB |
Output is correct |
14 |
Correct |
413 ms |
26868 KB |
Output is correct |
15 |
Correct |
215 ms |
19776 KB |
Output is correct |
16 |
Correct |
449 ms |
31216 KB |
Output is correct |
17 |
Correct |
538 ms |
28020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5120 KB |
Output is correct |
2 |
Correct |
11 ms |
5120 KB |
Output is correct |
3 |
Correct |
9 ms |
5120 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
12 ms |
5248 KB |
Output is correct |
6 |
Correct |
13 ms |
5376 KB |
Output is correct |
7 |
Correct |
9 ms |
5248 KB |
Output is correct |
8 |
Correct |
9 ms |
5120 KB |
Output is correct |
9 |
Correct |
9 ms |
5120 KB |
Output is correct |
10 |
Correct |
11 ms |
5120 KB |
Output is correct |
11 |
Correct |
9 ms |
5248 KB |
Output is correct |
12 |
Correct |
9 ms |
5120 KB |
Output is correct |
13 |
Correct |
9 ms |
5376 KB |
Output is correct |
14 |
Correct |
10 ms |
5248 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
9 ms |
5248 KB |
Output is correct |
17 |
Correct |
9 ms |
5248 KB |
Output is correct |
18 |
Correct |
9 ms |
5248 KB |
Output is correct |
19 |
Correct |
10 ms |
5248 KB |
Output is correct |
20 |
Correct |
10 ms |
5376 KB |
Output is correct |
21 |
Correct |
9 ms |
5224 KB |
Output is correct |
22 |
Correct |
9 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5248 KB |
Output is correct |
24 |
Correct |
9 ms |
5248 KB |
Output is correct |
25 |
Correct |
11 ms |
5224 KB |
Output is correct |
26 |
Correct |
11 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
5376 KB |
Output is correct |
28 |
Correct |
9 ms |
5248 KB |
Output is correct |
29 |
Correct |
10 ms |
5248 KB |
Output is correct |
30 |
Correct |
9 ms |
5248 KB |
Output is correct |
31 |
Correct |
9 ms |
5376 KB |
Output is correct |
32 |
Correct |
9 ms |
5248 KB |
Output is correct |
33 |
Correct |
178 ms |
12408 KB |
Output is correct |
34 |
Correct |
221 ms |
21752 KB |
Output is correct |
35 |
Correct |
37 ms |
7936 KB |
Output is correct |
36 |
Correct |
422 ms |
17860 KB |
Output is correct |
37 |
Correct |
459 ms |
34316 KB |
Output is correct |
38 |
Correct |
522 ms |
25848 KB |
Output is correct |
39 |
Correct |
342 ms |
17908 KB |
Output is correct |
40 |
Correct |
434 ms |
19576 KB |
Output is correct |
41 |
Correct |
388 ms |
19048 KB |
Output is correct |
42 |
Correct |
359 ms |
18936 KB |
Output is correct |
43 |
Correct |
207 ms |
18648 KB |
Output is correct |
44 |
Correct |
436 ms |
28024 KB |
Output is correct |
45 |
Correct |
379 ms |
25844 KB |
Output is correct |
46 |
Correct |
389 ms |
25364 KB |
Output is correct |
47 |
Correct |
195 ms |
18540 KB |
Output is correct |
48 |
Correct |
367 ms |
29040 KB |
Output is correct |
49 |
Correct |
444 ms |
26228 KB |
Output is correct |
50 |
Correct |
276 ms |
16504 KB |
Output is correct |
51 |
Correct |
480 ms |
35320 KB |
Output is correct |
52 |
Correct |
39 ms |
8568 KB |
Output is correct |
53 |
Correct |
386 ms |
19448 KB |
Output is correct |
54 |
Correct |
477 ms |
36728 KB |
Output is correct |
55 |
Correct |
490 ms |
27768 KB |
Output is correct |
56 |
Correct |
374 ms |
19576 KB |
Output is correct |
57 |
Correct |
474 ms |
23136 KB |
Output is correct |
58 |
Correct |
399 ms |
21880 KB |
Output is correct |
59 |
Correct |
460 ms |
20856 KB |
Output is correct |
60 |
Correct |
305 ms |
20080 KB |
Output is correct |
61 |
Correct |
458 ms |
33144 KB |
Output is correct |
62 |
Correct |
389 ms |
26740 KB |
Output is correct |
63 |
Correct |
413 ms |
26868 KB |
Output is correct |
64 |
Correct |
215 ms |
19776 KB |
Output is correct |
65 |
Correct |
449 ms |
31216 KB |
Output is correct |
66 |
Correct |
538 ms |
28020 KB |
Output is correct |
67 |
Correct |
34 ms |
6912 KB |
Output is correct |
68 |
Correct |
209 ms |
18432 KB |
Output is correct |
69 |
Correct |
288 ms |
18532 KB |
Output is correct |
70 |
Correct |
423 ms |
18008 KB |
Output is correct |
71 |
Correct |
567 ms |
34168 KB |
Output is correct |
72 |
Correct |
440 ms |
25720 KB |
Output is correct |
73 |
Correct |
431 ms |
17888 KB |
Output is correct |
74 |
Correct |
382 ms |
20856 KB |
Output is correct |
75 |
Correct |
381 ms |
19320 KB |
Output is correct |
76 |
Correct |
385 ms |
19192 KB |
Output is correct |
77 |
Correct |
267 ms |
18156 KB |
Output is correct |
78 |
Correct |
456 ms |
29560 KB |
Output is correct |
79 |
Correct |
403 ms |
27892 KB |
Output is correct |
80 |
Correct |
413 ms |
24308 KB |
Output is correct |
81 |
Correct |
173 ms |
18260 KB |
Output is correct |
82 |
Correct |
378 ms |
29040 KB |
Output is correct |
83 |
Correct |
406 ms |
25972 KB |
Output is correct |
84 |
Correct |
362 ms |
18168 KB |
Output is correct |
85 |
Correct |
486 ms |
34852 KB |
Output is correct |
86 |
Correct |
455 ms |
26232 KB |
Output is correct |
87 |
Correct |
358 ms |
18168 KB |
Output is correct |
88 |
Correct |
483 ms |
21624 KB |
Output is correct |
89 |
Correct |
381 ms |
19960 KB |
Output is correct |
90 |
Correct |
405 ms |
19576 KB |
Output is correct |
91 |
Correct |
282 ms |
18544 KB |
Output is correct |
92 |
Correct |
476 ms |
34552 KB |
Output is correct |
93 |
Correct |
388 ms |
24188 KB |
Output is correct |
94 |
Correct |
385 ms |
22264 KB |
Output is correct |
95 |
Correct |
170 ms |
18772 KB |
Output is correct |
96 |
Correct |
382 ms |
29560 KB |
Output is correct |
97 |
Correct |
390 ms |
26484 KB |
Output is correct |
98 |
Correct |
382 ms |
19496 KB |
Output is correct |
99 |
Correct |
496 ms |
35296 KB |
Output is correct |
100 |
Correct |
465 ms |
27384 KB |
Output is correct |
101 |
Correct |
353 ms |
18804 KB |
Output is correct |
102 |
Correct |
392 ms |
21624 KB |
Output is correct |
103 |
Correct |
378 ms |
20448 KB |
Output is correct |
104 |
Correct |
394 ms |
20216 KB |
Output is correct |
105 |
Correct |
265 ms |
18928 KB |
Output is correct |
106 |
Correct |
454 ms |
28572 KB |
Output is correct |
107 |
Correct |
388 ms |
28660 KB |
Output is correct |
108 |
Correct |
435 ms |
24568 KB |
Output is correct |
109 |
Correct |
187 ms |
19560 KB |
Output is correct |
110 |
Correct |
476 ms |
30448 KB |
Output is correct |
111 |
Correct |
488 ms |
27380 KB |
Output is correct |