#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int col[MAXN], cnt[MAXN];
vector <int> adj[MAXN];
int N, M, dep[MAXN], dis[MAXN];
int ans[MAXN], cur;
vector <int> st;
void DFS1(int u, int p) {
for (int v : adj[u]) if (v != p) {
dis[v] = dis[u] + 1; DFS1(v, u);
}
}
void DFS2(int u, int p) {
dep[u] = 0;
for (int v : adj[u]) if (v != p) {
DFS2(v, u);
dep[u] = max(dep[u], dep[v]);
}
dep[u]++;
}
void Add(int c) {
cnt[c]++;
if (cnt[c] == 1) cur++;
}
void Remove(int c) {
cnt[c]--;
if (cnt[c] == 0) cur--;
}
void Pop() {
Remove(col[st.back()]);
st.pop_back();
}
void Push(int u) {
Add(col[u]);
st.push_back(u);
}
void DFS3(int u, int p) {
vector <pair <int, int>> ch;
for (int v : adj[u]) if (v != p)
ch.emplace_back(dep[v], v);
if (ch.size()) {
swap(ch[0], *max_element(ch.begin(), ch.end()));
int len = ch.size() > 1 ? max_element
(ch.begin() + 1, ch.end())->first : 0;
for (auto c : ch) {
int v = c.second;
while (st.size() && dis[st.back()] >= dis[u] - len) Pop();
Push(u); DFS3(v, u);
if (st.size() && st.back() == u) Pop();
len = max(len, c.first);
}
while (st.size() && dis[st.back()] >= dis[u] - len) Pop();
}
ans[u] = max(ans[u], cur);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> N >> M;
for (int i = 1; i < N; i++) {
int u, v; cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= N; i++)
cin >> col[i];
dis[1] = 0; DFS1(1, 0);
int u = max_element(dis + 1, dis + N + 1) - dis;
dis[u] = 0; DFS1(u, 0);
DFS2(u, 0); DFS3(u, 0);
u = max_element(dis + 1, dis + N + 1) - dis;
dis[u] = 0; DFS1(u, 0);
DFS2(u, 0); DFS3(u, 0);
for (int i = 1; i <= N; i++)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
6 ms |
5196 KB |
Output is correct |
5 |
Correct |
6 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5432 KB |
Output is correct |
7 |
Correct |
5 ms |
5328 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
5 ms |
5176 KB |
Output is correct |
11 |
Correct |
5 ms |
5172 KB |
Output is correct |
12 |
Correct |
4 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5380 KB |
Output is correct |
14 |
Correct |
5 ms |
5292 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
5 ms |
5196 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
5 ms |
5324 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
5 ms |
5452 KB |
Output is correct |
21 |
Correct |
5 ms |
5256 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5172 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5436 KB |
Output is correct |
28 |
Correct |
5 ms |
5324 KB |
Output is correct |
29 |
Correct |
5 ms |
5324 KB |
Output is correct |
30 |
Correct |
5 ms |
5200 KB |
Output is correct |
31 |
Correct |
6 ms |
5324 KB |
Output is correct |
32 |
Correct |
5 ms |
5304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
12276 KB |
Output is correct |
2 |
Correct |
246 ms |
30588 KB |
Output is correct |
3 |
Correct |
44 ms |
9028 KB |
Output is correct |
4 |
Correct |
463 ms |
17816 KB |
Output is correct |
5 |
Correct |
602 ms |
49740 KB |
Output is correct |
6 |
Correct |
513 ms |
33584 KB |
Output is correct |
7 |
Correct |
394 ms |
18332 KB |
Output is correct |
8 |
Correct |
461 ms |
21160 KB |
Output is correct |
9 |
Correct |
461 ms |
20164 KB |
Output is correct |
10 |
Correct |
486 ms |
20016 KB |
Output is correct |
11 |
Correct |
205 ms |
21636 KB |
Output is correct |
12 |
Correct |
539 ms |
37692 KB |
Output is correct |
13 |
Correct |
489 ms |
33836 KB |
Output is correct |
14 |
Correct |
527 ms |
32868 KB |
Output is correct |
15 |
Correct |
174 ms |
21528 KB |
Output is correct |
16 |
Correct |
501 ms |
39928 KB |
Output is correct |
17 |
Correct |
501 ms |
34216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
356 ms |
16368 KB |
Output is correct |
2 |
Correct |
567 ms |
50212 KB |
Output is correct |
3 |
Correct |
55 ms |
9796 KB |
Output is correct |
4 |
Correct |
421 ms |
19472 KB |
Output is correct |
5 |
Correct |
647 ms |
52264 KB |
Output is correct |
6 |
Correct |
529 ms |
35524 KB |
Output is correct |
7 |
Correct |
431 ms |
19652 KB |
Output is correct |
8 |
Correct |
525 ms |
26192 KB |
Output is correct |
9 |
Correct |
508 ms |
24056 KB |
Output is correct |
10 |
Correct |
499 ms |
22084 KB |
Output is correct |
11 |
Correct |
344 ms |
21364 KB |
Output is correct |
12 |
Correct |
553 ms |
45624 KB |
Output is correct |
13 |
Correct |
508 ms |
33788 KB |
Output is correct |
14 |
Correct |
481 ms |
34076 KB |
Output is correct |
15 |
Correct |
196 ms |
22636 KB |
Output is correct |
16 |
Correct |
461 ms |
42128 KB |
Output is correct |
17 |
Correct |
487 ms |
36180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
5 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
6 ms |
5196 KB |
Output is correct |
5 |
Correct |
6 ms |
5068 KB |
Output is correct |
6 |
Correct |
5 ms |
5432 KB |
Output is correct |
7 |
Correct |
5 ms |
5328 KB |
Output is correct |
8 |
Correct |
5 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
5 ms |
5176 KB |
Output is correct |
11 |
Correct |
5 ms |
5172 KB |
Output is correct |
12 |
Correct |
4 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5380 KB |
Output is correct |
14 |
Correct |
5 ms |
5292 KB |
Output is correct |
15 |
Correct |
6 ms |
5196 KB |
Output is correct |
16 |
Correct |
5 ms |
5196 KB |
Output is correct |
17 |
Correct |
5 ms |
5324 KB |
Output is correct |
18 |
Correct |
5 ms |
5324 KB |
Output is correct |
19 |
Correct |
5 ms |
5068 KB |
Output is correct |
20 |
Correct |
5 ms |
5452 KB |
Output is correct |
21 |
Correct |
5 ms |
5256 KB |
Output is correct |
22 |
Correct |
5 ms |
5068 KB |
Output is correct |
23 |
Correct |
5 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5172 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5436 KB |
Output is correct |
28 |
Correct |
5 ms |
5324 KB |
Output is correct |
29 |
Correct |
5 ms |
5324 KB |
Output is correct |
30 |
Correct |
5 ms |
5200 KB |
Output is correct |
31 |
Correct |
6 ms |
5324 KB |
Output is correct |
32 |
Correct |
5 ms |
5304 KB |
Output is correct |
33 |
Correct |
186 ms |
12276 KB |
Output is correct |
34 |
Correct |
246 ms |
30588 KB |
Output is correct |
35 |
Correct |
44 ms |
9028 KB |
Output is correct |
36 |
Correct |
463 ms |
17816 KB |
Output is correct |
37 |
Correct |
602 ms |
49740 KB |
Output is correct |
38 |
Correct |
513 ms |
33584 KB |
Output is correct |
39 |
Correct |
394 ms |
18332 KB |
Output is correct |
40 |
Correct |
461 ms |
21160 KB |
Output is correct |
41 |
Correct |
461 ms |
20164 KB |
Output is correct |
42 |
Correct |
486 ms |
20016 KB |
Output is correct |
43 |
Correct |
205 ms |
21636 KB |
Output is correct |
44 |
Correct |
539 ms |
37692 KB |
Output is correct |
45 |
Correct |
489 ms |
33836 KB |
Output is correct |
46 |
Correct |
527 ms |
32868 KB |
Output is correct |
47 |
Correct |
174 ms |
21528 KB |
Output is correct |
48 |
Correct |
501 ms |
39928 KB |
Output is correct |
49 |
Correct |
501 ms |
34216 KB |
Output is correct |
50 |
Correct |
356 ms |
16368 KB |
Output is correct |
51 |
Correct |
567 ms |
50212 KB |
Output is correct |
52 |
Correct |
55 ms |
9796 KB |
Output is correct |
53 |
Correct |
421 ms |
19472 KB |
Output is correct |
54 |
Correct |
647 ms |
52264 KB |
Output is correct |
55 |
Correct |
529 ms |
35524 KB |
Output is correct |
56 |
Correct |
431 ms |
19652 KB |
Output is correct |
57 |
Correct |
525 ms |
26192 KB |
Output is correct |
58 |
Correct |
508 ms |
24056 KB |
Output is correct |
59 |
Correct |
499 ms |
22084 KB |
Output is correct |
60 |
Correct |
344 ms |
21364 KB |
Output is correct |
61 |
Correct |
553 ms |
45624 KB |
Output is correct |
62 |
Correct |
508 ms |
33788 KB |
Output is correct |
63 |
Correct |
481 ms |
34076 KB |
Output is correct |
64 |
Correct |
196 ms |
22636 KB |
Output is correct |
65 |
Correct |
461 ms |
42128 KB |
Output is correct |
66 |
Correct |
487 ms |
36180 KB |
Output is correct |
67 |
Correct |
29 ms |
6880 KB |
Output is correct |
68 |
Correct |
162 ms |
24992 KB |
Output is correct |
69 |
Correct |
255 ms |
23456 KB |
Output is correct |
70 |
Correct |
470 ms |
17872 KB |
Output is correct |
71 |
Correct |
562 ms |
49716 KB |
Output is correct |
72 |
Correct |
533 ms |
33648 KB |
Output is correct |
73 |
Correct |
499 ms |
17932 KB |
Output is correct |
74 |
Correct |
452 ms |
24004 KB |
Output is correct |
75 |
Correct |
447 ms |
20900 KB |
Output is correct |
76 |
Correct |
458 ms |
20348 KB |
Output is correct |
77 |
Correct |
307 ms |
19748 KB |
Output is correct |
78 |
Correct |
531 ms |
41136 KB |
Output is correct |
79 |
Correct |
503 ms |
37588 KB |
Output is correct |
80 |
Correct |
500 ms |
30836 KB |
Output is correct |
81 |
Correct |
188 ms |
21364 KB |
Output is correct |
82 |
Correct |
445 ms |
39940 KB |
Output is correct |
83 |
Correct |
458 ms |
34292 KB |
Output is correct |
84 |
Correct |
442 ms |
18200 KB |
Output is correct |
85 |
Correct |
552 ms |
50360 KB |
Output is correct |
86 |
Correct |
535 ms |
34244 KB |
Output is correct |
87 |
Correct |
413 ms |
18244 KB |
Output is correct |
88 |
Correct |
472 ms |
24920 KB |
Output is correct |
89 |
Correct |
429 ms |
21748 KB |
Output is correct |
90 |
Correct |
485 ms |
20928 KB |
Output is correct |
91 |
Correct |
318 ms |
20028 KB |
Output is correct |
92 |
Correct |
582 ms |
49360 KB |
Output is correct |
93 |
Correct |
461 ms |
29716 KB |
Output is correct |
94 |
Correct |
478 ms |
26492 KB |
Output is correct |
95 |
Correct |
191 ms |
21976 KB |
Output is correct |
96 |
Correct |
547 ms |
40516 KB |
Output is correct |
97 |
Correct |
550 ms |
34692 KB |
Output is correct |
98 |
Correct |
470 ms |
19504 KB |
Output is correct |
99 |
Correct |
627 ms |
50928 KB |
Output is correct |
100 |
Correct |
597 ms |
35228 KB |
Output is correct |
101 |
Correct |
425 ms |
19132 KB |
Output is correct |
102 |
Correct |
543 ms |
24356 KB |
Output is correct |
103 |
Correct |
463 ms |
22064 KB |
Output is correct |
104 |
Correct |
506 ms |
21296 KB |
Output is correct |
105 |
Correct |
327 ms |
20456 KB |
Output is correct |
106 |
Correct |
585 ms |
37136 KB |
Output is correct |
107 |
Correct |
514 ms |
38112 KB |
Output is correct |
108 |
Correct |
497 ms |
29568 KB |
Output is correct |
109 |
Correct |
195 ms |
22520 KB |
Output is correct |
110 |
Correct |
493 ms |
41244 KB |
Output is correct |
111 |
Correct |
502 ms |
35712 KB |
Output is correct |