#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 2e5 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, m, k, color[N], ptr;
string s;
vector<int> adj[N];
int dp[N], depth[N];
void dfs_len(int u, int p = 0) {
dp[u] = depth[u] = depth[p] + 1;
if (depth[ptr] < depth[u]) ptr = u;
for (int v : adj[u]) if (v != p) {
dfs_len(v, u);
dp[u] = max(dp[u], dp[v]);
}
}
int cnt[N], cnt_unique, ans[N];
vector<int> A;
void add(int x) {
if (++cnt[color[x]] == 1) ++cnt_unique;
A.push_back(x);
}
void del() {
int x = A.back(); A.pop_back();
if (--cnt[color[x]] == 0) --cnt_unique;
}
void dfs(int u, int p = 0) {
if (p) add(p);
vector<ii> vec;
for (int v : adj[u]) if (v != p) vec.emplace_back(dp[v], v);
sort(vec.begin(), vec.end(), greater<ii>());
int mx1 = vec.size() > 0 ? vec[0].fi : 0;
int mx2 = vec.size() > 1 ? vec[1].fi : 0;
for (ii v : vec) {
int mx = mx1 + mx2 - max(v.fi, mx2);
while (!A.empty() && mx - depth[u] >= depth[u] - depth[A.back()]) del();
dfs(v.se, u);
}
while (!A.empty() && dp[u] - depth[u] >= depth[u] - depth[A.back()]) del();
ans[u] = max(ans[u], cnt_unique);
}
void process() {
cin >> n >> m;
foru(i,1,n-1) {
int u, v; cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
foru(i,1,n) cin >> color[i];
dfs_len(ptr=1);
int i;
foru(k,0,1) {
dfs_len(i=ptr);
dfs(i);
}
foru(i,1,n) cout << ans[i] << ' ';
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6820 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
7000 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6712 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6772 KB |
Output is correct |
13 |
Correct |
2 ms |
7004 KB |
Output is correct |
14 |
Correct |
2 ms |
6844 KB |
Output is correct |
15 |
Correct |
3 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
6712 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6636 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6744 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
12840 KB |
Output is correct |
2 |
Correct |
94 ms |
29352 KB |
Output is correct |
3 |
Correct |
17 ms |
10172 KB |
Output is correct |
4 |
Correct |
139 ms |
17968 KB |
Output is correct |
5 |
Correct |
237 ms |
46680 KB |
Output is correct |
6 |
Correct |
267 ms |
32100 KB |
Output is correct |
7 |
Correct |
129 ms |
18216 KB |
Output is correct |
8 |
Correct |
237 ms |
20980 KB |
Output is correct |
9 |
Correct |
141 ms |
20052 KB |
Output is correct |
10 |
Correct |
164 ms |
19824 KB |
Output is correct |
11 |
Correct |
115 ms |
21756 KB |
Output is correct |
12 |
Correct |
238 ms |
36040 KB |
Output is correct |
13 |
Correct |
212 ms |
32264 KB |
Output is correct |
14 |
Correct |
185 ms |
31592 KB |
Output is correct |
15 |
Correct |
102 ms |
21788 KB |
Output is correct |
16 |
Correct |
195 ms |
37984 KB |
Output is correct |
17 |
Correct |
200 ms |
32828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
16688 KB |
Output is correct |
2 |
Correct |
218 ms |
47428 KB |
Output is correct |
3 |
Correct |
19 ms |
10840 KB |
Output is correct |
4 |
Correct |
164 ms |
19512 KB |
Output is correct |
5 |
Correct |
291 ms |
49284 KB |
Output is correct |
6 |
Correct |
205 ms |
33996 KB |
Output is correct |
7 |
Correct |
192 ms |
19800 KB |
Output is correct |
8 |
Correct |
163 ms |
25688 KB |
Output is correct |
9 |
Correct |
160 ms |
23572 KB |
Output is correct |
10 |
Correct |
150 ms |
21800 KB |
Output is correct |
11 |
Correct |
139 ms |
21208 KB |
Output is correct |
12 |
Correct |
229 ms |
43336 KB |
Output is correct |
13 |
Correct |
187 ms |
32512 KB |
Output is correct |
14 |
Correct |
207 ms |
32744 KB |
Output is correct |
15 |
Correct |
127 ms |
22684 KB |
Output is correct |
16 |
Correct |
249 ms |
39996 KB |
Output is correct |
17 |
Correct |
230 ms |
34624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6820 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
7000 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6712 KB |
Output is correct |
11 |
Correct |
2 ms |
6744 KB |
Output is correct |
12 |
Correct |
2 ms |
6772 KB |
Output is correct |
13 |
Correct |
2 ms |
7004 KB |
Output is correct |
14 |
Correct |
2 ms |
6844 KB |
Output is correct |
15 |
Correct |
3 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6744 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
3 ms |
6712 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
7004 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6636 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
2 ms |
6748 KB |
Output is correct |
26 |
Correct |
2 ms |
6744 KB |
Output is correct |
27 |
Correct |
2 ms |
6748 KB |
Output is correct |
28 |
Correct |
2 ms |
6748 KB |
Output is correct |
29 |
Correct |
2 ms |
6748 KB |
Output is correct |
30 |
Correct |
2 ms |
6748 KB |
Output is correct |
31 |
Correct |
2 ms |
6748 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
62 ms |
12840 KB |
Output is correct |
34 |
Correct |
94 ms |
29352 KB |
Output is correct |
35 |
Correct |
17 ms |
10172 KB |
Output is correct |
36 |
Correct |
139 ms |
17968 KB |
Output is correct |
37 |
Correct |
237 ms |
46680 KB |
Output is correct |
38 |
Correct |
267 ms |
32100 KB |
Output is correct |
39 |
Correct |
129 ms |
18216 KB |
Output is correct |
40 |
Correct |
237 ms |
20980 KB |
Output is correct |
41 |
Correct |
141 ms |
20052 KB |
Output is correct |
42 |
Correct |
164 ms |
19824 KB |
Output is correct |
43 |
Correct |
115 ms |
21756 KB |
Output is correct |
44 |
Correct |
238 ms |
36040 KB |
Output is correct |
45 |
Correct |
212 ms |
32264 KB |
Output is correct |
46 |
Correct |
185 ms |
31592 KB |
Output is correct |
47 |
Correct |
102 ms |
21788 KB |
Output is correct |
48 |
Correct |
195 ms |
37984 KB |
Output is correct |
49 |
Correct |
200 ms |
32828 KB |
Output is correct |
50 |
Correct |
93 ms |
16688 KB |
Output is correct |
51 |
Correct |
218 ms |
47428 KB |
Output is correct |
52 |
Correct |
19 ms |
10840 KB |
Output is correct |
53 |
Correct |
164 ms |
19512 KB |
Output is correct |
54 |
Correct |
291 ms |
49284 KB |
Output is correct |
55 |
Correct |
205 ms |
33996 KB |
Output is correct |
56 |
Correct |
192 ms |
19800 KB |
Output is correct |
57 |
Correct |
163 ms |
25688 KB |
Output is correct |
58 |
Correct |
160 ms |
23572 KB |
Output is correct |
59 |
Correct |
150 ms |
21800 KB |
Output is correct |
60 |
Correct |
139 ms |
21208 KB |
Output is correct |
61 |
Correct |
229 ms |
43336 KB |
Output is correct |
62 |
Correct |
187 ms |
32512 KB |
Output is correct |
63 |
Correct |
207 ms |
32744 KB |
Output is correct |
64 |
Correct |
127 ms |
22684 KB |
Output is correct |
65 |
Correct |
249 ms |
39996 KB |
Output is correct |
66 |
Correct |
230 ms |
34624 KB |
Output is correct |
67 |
Correct |
16 ms |
8536 KB |
Output is correct |
68 |
Correct |
69 ms |
24544 KB |
Output is correct |
69 |
Correct |
99 ms |
22916 KB |
Output is correct |
70 |
Correct |
134 ms |
18004 KB |
Output is correct |
71 |
Correct |
287 ms |
46976 KB |
Output is correct |
72 |
Correct |
227 ms |
31944 KB |
Output is correct |
73 |
Correct |
173 ms |
18048 KB |
Output is correct |
74 |
Correct |
215 ms |
23632 KB |
Output is correct |
75 |
Correct |
167 ms |
20620 KB |
Output is correct |
76 |
Correct |
155 ms |
20260 KB |
Output is correct |
77 |
Correct |
140 ms |
19772 KB |
Output is correct |
78 |
Correct |
328 ms |
38844 KB |
Output is correct |
79 |
Correct |
217 ms |
36264 KB |
Output is correct |
80 |
Correct |
213 ms |
30092 KB |
Output is correct |
81 |
Correct |
103 ms |
21544 KB |
Output is correct |
82 |
Correct |
251 ms |
37880 KB |
Output is correct |
83 |
Correct |
191 ms |
32576 KB |
Output is correct |
84 |
Correct |
133 ms |
18256 KB |
Output is correct |
85 |
Correct |
285 ms |
47344 KB |
Output is correct |
86 |
Correct |
224 ms |
32464 KB |
Output is correct |
87 |
Correct |
132 ms |
18248 KB |
Output is correct |
88 |
Correct |
171 ms |
24388 KB |
Output is correct |
89 |
Correct |
141 ms |
21440 KB |
Output is correct |
90 |
Correct |
164 ms |
21012 KB |
Output is correct |
91 |
Correct |
197 ms |
20204 KB |
Output is correct |
92 |
Correct |
274 ms |
46480 KB |
Output is correct |
93 |
Correct |
215 ms |
28688 KB |
Output is correct |
94 |
Correct |
170 ms |
26128 KB |
Output is correct |
95 |
Correct |
108 ms |
22072 KB |
Output is correct |
96 |
Correct |
194 ms |
38324 KB |
Output is correct |
97 |
Correct |
231 ms |
33236 KB |
Output is correct |
98 |
Correct |
152 ms |
19540 KB |
Output is correct |
99 |
Correct |
289 ms |
47872 KB |
Output is correct |
100 |
Correct |
227 ms |
33740 KB |
Output is correct |
101 |
Correct |
204 ms |
19528 KB |
Output is correct |
102 |
Correct |
168 ms |
23636 KB |
Output is correct |
103 |
Correct |
169 ms |
21756 KB |
Output is correct |
104 |
Correct |
171 ms |
21076 KB |
Output is correct |
105 |
Correct |
178 ms |
20588 KB |
Output is correct |
106 |
Correct |
226 ms |
35536 KB |
Output is correct |
107 |
Correct |
205 ms |
36484 KB |
Output is correct |
108 |
Correct |
199 ms |
28532 KB |
Output is correct |
109 |
Correct |
130 ms |
22756 KB |
Output is correct |
110 |
Correct |
220 ms |
38836 KB |
Output is correct |
111 |
Correct |
224 ms |
34116 KB |
Output is correct |