#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> adj[200200], st;
int n, m, c[200200], d[200200], h[200200], ans[200200], cnt[200200], dist[200200], cur;
void dfs0(int s, int pa = -1){
if (pa!=-1) d[s] = d[pa] + 1;
else d[s] = 1;
h[s] = 1;
for (auto &v:adj[s]) if (v!=pa){
dfs0(v, s);
h[s] = max(h[s], h[v]+1);
}
}
void _insert(int s){
if (st.empty() || st.back()!=s){
st.push_back(s);
if (!cnt[c[s]]) cur++;
cnt[c[s]]++;
}
}
void _pop(int s, int x){
while(!st.empty() && d[s] - d[st.back()] <= x){
--cnt[c[st.back()]];
if (!cnt[c[st.back()]]) cur--;
st.pop_back();
}
}
void update(int s){
if (dist[s] < d[s]){
dist[s] = d[s];
ans[s] = cur;
}
}
void dfs(int s, int pa = -1){
vector<int> deep; ///max and second max
for (auto &v:adj[s]) if (v!=pa){
deep.push_back(v);
for (int i=(int)deep.size()-1;i;i--){
if (h[deep[i-1]] >= h[deep[i]]) break;
swap(deep[i-1], deep[i]);
}
if (deep.size()>2) deep.pop_back();
}
if (deep.empty()){update(s); return;}
if (deep.size()==2) _pop(s, h[deep[1]]); ///pop second max
_insert(s);
dfs(deep[0], s); ///traverse max first
_pop(s, h[deep[0]]); ///pop all
for (auto &v:adj[s]) if (v!=pa && v!=deep[0]){
_insert(s);
dfs(v, s);
}
_pop(s, 0);
update(s);
}
int main(){
scanf("%d %d", &n, &m);
for (int i=1;i<=n-1;i++){
int x, y;
scanf("%d %d", &x, &y);
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i=1;i<=n;i++) scanf("%d", c+i);
dfs0(1);
int r1 = max_element(d+1, d+n+1) - d;
dfs0(r1);
dfs(r1);
/*printf("r1 = %d\n", r1);
for (int i=1;i<=n;i++) printf("%d\n", ans[i]);*/
int r2 = max_element(d+1, d+n+1) - d;
dfs0(r2);
dfs(r2);
//printf("r2 = %d\n", r2);
for (int i=1;i<=n;i++) printf("%d\n", ans[i]);
return 0;
}
Compilation message
joi2019_ho_t5.cpp: In function 'int main()':
joi2019_ho_t5.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp:77:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | for (int i=1;i<=n;i++) scanf("%d", c+i);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5208 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5148 KB |
Output is correct |
6 |
Correct |
4 ms |
5404 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
5 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5172 KB |
Output is correct |
11 |
Correct |
5 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5148 KB |
Output is correct |
13 |
Correct |
4 ms |
5332 KB |
Output is correct |
14 |
Correct |
8 ms |
5148 KB |
Output is correct |
15 |
Correct |
7 ms |
5204 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
8 ms |
5340 KB |
Output is correct |
18 |
Correct |
5 ms |
5204 KB |
Output is correct |
19 |
Correct |
6 ms |
5076 KB |
Output is correct |
20 |
Correct |
4 ms |
5460 KB |
Output is correct |
21 |
Correct |
5 ms |
5268 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
6 ms |
5148 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
6 ms |
5092 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5332 KB |
Output is correct |
28 |
Correct |
5 ms |
5332 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
8 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
11216 KB |
Output is correct |
2 |
Correct |
190 ms |
28028 KB |
Output is correct |
3 |
Correct |
29 ms |
8728 KB |
Output is correct |
4 |
Correct |
322 ms |
16076 KB |
Output is correct |
5 |
Correct |
439 ms |
44780 KB |
Output is correct |
6 |
Correct |
435 ms |
30044 KB |
Output is correct |
7 |
Correct |
297 ms |
16008 KB |
Output is correct |
8 |
Correct |
333 ms |
18704 KB |
Output is correct |
9 |
Correct |
305 ms |
17884 KB |
Output is correct |
10 |
Correct |
315 ms |
17772 KB |
Output is correct |
11 |
Correct |
154 ms |
16548 KB |
Output is correct |
12 |
Correct |
404 ms |
33704 KB |
Output is correct |
13 |
Correct |
336 ms |
29916 KB |
Output is correct |
14 |
Correct |
403 ms |
29000 KB |
Output is correct |
15 |
Correct |
124 ms |
16516 KB |
Output is correct |
16 |
Correct |
375 ms |
35416 KB |
Output is correct |
17 |
Correct |
353 ms |
30292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
14076 KB |
Output is correct |
2 |
Correct |
490 ms |
44720 KB |
Output is correct |
3 |
Correct |
31 ms |
9292 KB |
Output is correct |
4 |
Correct |
328 ms |
16868 KB |
Output is correct |
5 |
Correct |
453 ms |
46580 KB |
Output is correct |
6 |
Correct |
380 ms |
31284 KB |
Output is correct |
7 |
Correct |
332 ms |
16892 KB |
Output is correct |
8 |
Correct |
399 ms |
22740 KB |
Output is correct |
9 |
Correct |
371 ms |
20704 KB |
Output is correct |
10 |
Correct |
353 ms |
19068 KB |
Output is correct |
11 |
Correct |
236 ms |
17128 KB |
Output is correct |
12 |
Correct |
426 ms |
40464 KB |
Output is correct |
13 |
Correct |
339 ms |
29356 KB |
Output is correct |
14 |
Correct |
359 ms |
29552 KB |
Output is correct |
15 |
Correct |
192 ms |
16572 KB |
Output is correct |
16 |
Correct |
331 ms |
36716 KB |
Output is correct |
17 |
Correct |
416 ms |
31304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5208 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
7 ms |
5148 KB |
Output is correct |
6 |
Correct |
4 ms |
5404 KB |
Output is correct |
7 |
Correct |
5 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
5 ms |
5076 KB |
Output is correct |
10 |
Correct |
5 ms |
5172 KB |
Output is correct |
11 |
Correct |
5 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5148 KB |
Output is correct |
13 |
Correct |
4 ms |
5332 KB |
Output is correct |
14 |
Correct |
8 ms |
5148 KB |
Output is correct |
15 |
Correct |
7 ms |
5204 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
8 ms |
5340 KB |
Output is correct |
18 |
Correct |
5 ms |
5204 KB |
Output is correct |
19 |
Correct |
6 ms |
5076 KB |
Output is correct |
20 |
Correct |
4 ms |
5460 KB |
Output is correct |
21 |
Correct |
5 ms |
5268 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
6 ms |
5148 KB |
Output is correct |
24 |
Correct |
5 ms |
5076 KB |
Output is correct |
25 |
Correct |
6 ms |
5092 KB |
Output is correct |
26 |
Correct |
5 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5332 KB |
Output is correct |
28 |
Correct |
5 ms |
5332 KB |
Output is correct |
29 |
Correct |
5 ms |
5204 KB |
Output is correct |
30 |
Correct |
4 ms |
5076 KB |
Output is correct |
31 |
Correct |
8 ms |
5332 KB |
Output is correct |
32 |
Correct |
4 ms |
5204 KB |
Output is correct |
33 |
Correct |
158 ms |
11216 KB |
Output is correct |
34 |
Correct |
190 ms |
28028 KB |
Output is correct |
35 |
Correct |
29 ms |
8728 KB |
Output is correct |
36 |
Correct |
322 ms |
16076 KB |
Output is correct |
37 |
Correct |
439 ms |
44780 KB |
Output is correct |
38 |
Correct |
435 ms |
30044 KB |
Output is correct |
39 |
Correct |
297 ms |
16008 KB |
Output is correct |
40 |
Correct |
333 ms |
18704 KB |
Output is correct |
41 |
Correct |
305 ms |
17884 KB |
Output is correct |
42 |
Correct |
315 ms |
17772 KB |
Output is correct |
43 |
Correct |
154 ms |
16548 KB |
Output is correct |
44 |
Correct |
404 ms |
33704 KB |
Output is correct |
45 |
Correct |
336 ms |
29916 KB |
Output is correct |
46 |
Correct |
403 ms |
29000 KB |
Output is correct |
47 |
Correct |
124 ms |
16516 KB |
Output is correct |
48 |
Correct |
375 ms |
35416 KB |
Output is correct |
49 |
Correct |
353 ms |
30292 KB |
Output is correct |
50 |
Correct |
247 ms |
14076 KB |
Output is correct |
51 |
Correct |
490 ms |
44720 KB |
Output is correct |
52 |
Correct |
31 ms |
9292 KB |
Output is correct |
53 |
Correct |
328 ms |
16868 KB |
Output is correct |
54 |
Correct |
453 ms |
46580 KB |
Output is correct |
55 |
Correct |
380 ms |
31284 KB |
Output is correct |
56 |
Correct |
332 ms |
16892 KB |
Output is correct |
57 |
Correct |
399 ms |
22740 KB |
Output is correct |
58 |
Correct |
371 ms |
20704 KB |
Output is correct |
59 |
Correct |
353 ms |
19068 KB |
Output is correct |
60 |
Correct |
236 ms |
17128 KB |
Output is correct |
61 |
Correct |
426 ms |
40464 KB |
Output is correct |
62 |
Correct |
339 ms |
29356 KB |
Output is correct |
63 |
Correct |
359 ms |
29552 KB |
Output is correct |
64 |
Correct |
192 ms |
16572 KB |
Output is correct |
65 |
Correct |
331 ms |
36716 KB |
Output is correct |
66 |
Correct |
416 ms |
31304 KB |
Output is correct |
67 |
Correct |
29 ms |
6772 KB |
Output is correct |
68 |
Correct |
166 ms |
22760 KB |
Output is correct |
69 |
Correct |
228 ms |
20904 KB |
Output is correct |
70 |
Correct |
311 ms |
15908 KB |
Output is correct |
71 |
Correct |
455 ms |
44672 KB |
Output is correct |
72 |
Correct |
366 ms |
30024 KB |
Output is correct |
73 |
Correct |
288 ms |
15928 KB |
Output is correct |
74 |
Correct |
325 ms |
21292 KB |
Output is correct |
75 |
Correct |
313 ms |
18428 KB |
Output is correct |
76 |
Correct |
317 ms |
18084 KB |
Output is correct |
77 |
Correct |
255 ms |
16292 KB |
Output is correct |
78 |
Correct |
373 ms |
36784 KB |
Output is correct |
79 |
Correct |
374 ms |
33232 KB |
Output is correct |
80 |
Correct |
327 ms |
27496 KB |
Output is correct |
81 |
Correct |
129 ms |
16504 KB |
Output is correct |
82 |
Correct |
340 ms |
35312 KB |
Output is correct |
83 |
Correct |
328 ms |
30224 KB |
Output is correct |
84 |
Correct |
316 ms |
15964 KB |
Output is correct |
85 |
Correct |
426 ms |
45164 KB |
Output is correct |
86 |
Correct |
448 ms |
30180 KB |
Output is correct |
87 |
Correct |
296 ms |
16088 KB |
Output is correct |
88 |
Correct |
335 ms |
21824 KB |
Output is correct |
89 |
Correct |
351 ms |
18952 KB |
Output is correct |
90 |
Correct |
301 ms |
18244 KB |
Output is correct |
91 |
Correct |
230 ms |
16404 KB |
Output is correct |
92 |
Correct |
393 ms |
44096 KB |
Output is correct |
93 |
Correct |
309 ms |
25672 KB |
Output is correct |
94 |
Correct |
310 ms |
23276 KB |
Output is correct |
95 |
Correct |
122 ms |
16576 KB |
Output is correct |
96 |
Correct |
317 ms |
35776 KB |
Output is correct |
97 |
Correct |
332 ms |
30408 KB |
Output is correct |
98 |
Correct |
295 ms |
16748 KB |
Output is correct |
99 |
Correct |
398 ms |
45304 KB |
Output is correct |
100 |
Correct |
381 ms |
31020 KB |
Output is correct |
101 |
Correct |
267 ms |
16120 KB |
Output is correct |
102 |
Correct |
321 ms |
20856 KB |
Output is correct |
103 |
Correct |
299 ms |
18864 KB |
Output is correct |
104 |
Correct |
300 ms |
18424 KB |
Output is correct |
105 |
Correct |
208 ms |
16364 KB |
Output is correct |
106 |
Correct |
363 ms |
32584 KB |
Output is correct |
107 |
Correct |
330 ms |
33172 KB |
Output is correct |
108 |
Correct |
329 ms |
25464 KB |
Output is correct |
109 |
Correct |
132 ms |
16680 KB |
Output is correct |
110 |
Correct |
337 ms |
36112 KB |
Output is correct |
111 |
Correct |
331 ms |
30996 KB |
Output is correct |