// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 2e5+5;
int n, k, c[maxn], sz[maxn], par[maxn], id[maxn];
bool mark[maxn], vis[maxn];
vector <int> adj[maxn], vec[maxn];
queue <int> q;
void dfs1(int v, int p) {
sz[v] = 1;
vis[c[v]] = 1;
for (int u : adj[v]) {
if (!mark[u] and u != p) {
dfs1(u, v); sz[v] += sz[u];
}
}
}
void dfs2(int v, int p, int r) {
par[v] = p, id[v] = r;
for (int u : adj[v]) {
if (!mark[u] and u != p) dfs2(u, v, r);
}
}
int fcen(int v, int p, int s) {
for (int u : adj[v]) {
if (!mark[u] and u != p and sz[u]*2 > s) return fcen(u, v, s);
}
return v;
}
int cen(int v) {
dfs1(v, v);
v = fcen(v, v, sz[v]);
dfs2(v, v, v);
int ans = 0;
q.push(c[v]);
while (q.size()) {
int col = q.front(); q.pop();
if (!vis[col]) continue;
vis[col] = 0;
for (int u : vec[col]) {
if (id[u] != v) {
ans = k; break;
}
q.push(c[par[u]]);
}
ans++;
}
mark[v] = 1;
for (int u : adj[v]) {
if (!mark[u]) smin(ans, cen(u));
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int v, u; cin >> v >> u;
adj[--v].pb(--u);
adj[u].pb(v);
}
for (int i = 0; i < n; i++) {
cin >> c[i]; vec[--c[i]].pb(i);
}
cout << cen(0)-1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
2 ms |
12880 KB |
Output is correct |
4 |
Correct |
2 ms |
12880 KB |
Output is correct |
5 |
Correct |
2 ms |
12916 KB |
Output is correct |
6 |
Correct |
3 ms |
12880 KB |
Output is correct |
7 |
Correct |
2 ms |
12880 KB |
Output is correct |
8 |
Correct |
3 ms |
12880 KB |
Output is correct |
9 |
Correct |
3 ms |
12880 KB |
Output is correct |
10 |
Correct |
2 ms |
12952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
2 ms |
12880 KB |
Output is correct |
4 |
Correct |
2 ms |
12880 KB |
Output is correct |
5 |
Correct |
2 ms |
12916 KB |
Output is correct |
6 |
Correct |
3 ms |
12880 KB |
Output is correct |
7 |
Correct |
2 ms |
12880 KB |
Output is correct |
8 |
Correct |
3 ms |
12880 KB |
Output is correct |
9 |
Correct |
3 ms |
12880 KB |
Output is correct |
10 |
Correct |
2 ms |
12952 KB |
Output is correct |
11 |
Correct |
4 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
3 ms |
12880 KB |
Output is correct |
15 |
Correct |
3 ms |
13052 KB |
Output is correct |
16 |
Correct |
4 ms |
12880 KB |
Output is correct |
17 |
Correct |
4 ms |
12880 KB |
Output is correct |
18 |
Correct |
3 ms |
12880 KB |
Output is correct |
19 |
Correct |
3 ms |
12888 KB |
Output is correct |
20 |
Correct |
3 ms |
12880 KB |
Output is correct |
21 |
Correct |
4 ms |
12880 KB |
Output is correct |
22 |
Correct |
3 ms |
13136 KB |
Output is correct |
23 |
Correct |
4 ms |
12880 KB |
Output is correct |
24 |
Correct |
4 ms |
13084 KB |
Output is correct |
25 |
Correct |
4 ms |
12880 KB |
Output is correct |
26 |
Correct |
4 ms |
13136 KB |
Output is correct |
27 |
Correct |
4 ms |
12880 KB |
Output is correct |
28 |
Correct |
4 ms |
13116 KB |
Output is correct |
29 |
Correct |
4 ms |
12880 KB |
Output is correct |
30 |
Correct |
6 ms |
12880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
34640 KB |
Output is correct |
2 |
Correct |
139 ms |
35144 KB |
Output is correct |
3 |
Correct |
225 ms |
34440 KB |
Output is correct |
4 |
Correct |
125 ms |
35144 KB |
Output is correct |
5 |
Correct |
227 ms |
32020 KB |
Output is correct |
6 |
Correct |
122 ms |
35144 KB |
Output is correct |
7 |
Correct |
254 ms |
32404 KB |
Output is correct |
8 |
Correct |
133 ms |
35012 KB |
Output is correct |
9 |
Correct |
292 ms |
31088 KB |
Output is correct |
10 |
Correct |
293 ms |
29064 KB |
Output is correct |
11 |
Correct |
276 ms |
31392 KB |
Output is correct |
12 |
Correct |
248 ms |
33252 KB |
Output is correct |
13 |
Correct |
241 ms |
28676 KB |
Output is correct |
14 |
Correct |
261 ms |
33788 KB |
Output is correct |
15 |
Correct |
254 ms |
33352 KB |
Output is correct |
16 |
Correct |
266 ms |
29500 KB |
Output is correct |
17 |
Correct |
290 ms |
29768 KB |
Output is correct |
18 |
Correct |
237 ms |
30024 KB |
Output is correct |
19 |
Correct |
242 ms |
32568 KB |
Output is correct |
20 |
Correct |
264 ms |
34376 KB |
Output is correct |
21 |
Correct |
3 ms |
12880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
2 ms |
12880 KB |
Output is correct |
4 |
Correct |
2 ms |
12880 KB |
Output is correct |
5 |
Correct |
2 ms |
12916 KB |
Output is correct |
6 |
Correct |
3 ms |
12880 KB |
Output is correct |
7 |
Correct |
2 ms |
12880 KB |
Output is correct |
8 |
Correct |
3 ms |
12880 KB |
Output is correct |
9 |
Correct |
3 ms |
12880 KB |
Output is correct |
10 |
Correct |
2 ms |
12952 KB |
Output is correct |
11 |
Correct |
4 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
3 ms |
12880 KB |
Output is correct |
15 |
Correct |
3 ms |
13052 KB |
Output is correct |
16 |
Correct |
4 ms |
12880 KB |
Output is correct |
17 |
Correct |
4 ms |
12880 KB |
Output is correct |
18 |
Correct |
3 ms |
12880 KB |
Output is correct |
19 |
Correct |
3 ms |
12888 KB |
Output is correct |
20 |
Correct |
3 ms |
12880 KB |
Output is correct |
21 |
Correct |
4 ms |
12880 KB |
Output is correct |
22 |
Correct |
3 ms |
13136 KB |
Output is correct |
23 |
Correct |
4 ms |
12880 KB |
Output is correct |
24 |
Correct |
4 ms |
13084 KB |
Output is correct |
25 |
Correct |
4 ms |
12880 KB |
Output is correct |
26 |
Correct |
4 ms |
13136 KB |
Output is correct |
27 |
Correct |
4 ms |
12880 KB |
Output is correct |
28 |
Correct |
4 ms |
13116 KB |
Output is correct |
29 |
Correct |
4 ms |
12880 KB |
Output is correct |
30 |
Correct |
6 ms |
12880 KB |
Output is correct |
31 |
Correct |
289 ms |
34640 KB |
Output is correct |
32 |
Correct |
139 ms |
35144 KB |
Output is correct |
33 |
Correct |
225 ms |
34440 KB |
Output is correct |
34 |
Correct |
125 ms |
35144 KB |
Output is correct |
35 |
Correct |
227 ms |
32020 KB |
Output is correct |
36 |
Correct |
122 ms |
35144 KB |
Output is correct |
37 |
Correct |
254 ms |
32404 KB |
Output is correct |
38 |
Correct |
133 ms |
35012 KB |
Output is correct |
39 |
Correct |
292 ms |
31088 KB |
Output is correct |
40 |
Correct |
293 ms |
29064 KB |
Output is correct |
41 |
Correct |
276 ms |
31392 KB |
Output is correct |
42 |
Correct |
248 ms |
33252 KB |
Output is correct |
43 |
Correct |
241 ms |
28676 KB |
Output is correct |
44 |
Correct |
261 ms |
33788 KB |
Output is correct |
45 |
Correct |
254 ms |
33352 KB |
Output is correct |
46 |
Correct |
266 ms |
29500 KB |
Output is correct |
47 |
Correct |
290 ms |
29768 KB |
Output is correct |
48 |
Correct |
237 ms |
30024 KB |
Output is correct |
49 |
Correct |
242 ms |
32568 KB |
Output is correct |
50 |
Correct |
264 ms |
34376 KB |
Output is correct |
51 |
Correct |
3 ms |
12880 KB |
Output is correct |
52 |
Correct |
160 ms |
21976 KB |
Output is correct |
53 |
Correct |
175 ms |
21952 KB |
Output is correct |
54 |
Correct |
201 ms |
21832 KB |
Output is correct |
55 |
Correct |
193 ms |
21796 KB |
Output is correct |
56 |
Correct |
190 ms |
21840 KB |
Output is correct |
57 |
Correct |
170 ms |
21832 KB |
Output is correct |
58 |
Correct |
184 ms |
24136 KB |
Output is correct |
59 |
Correct |
186 ms |
24564 KB |
Output is correct |
60 |
Correct |
211 ms |
23880 KB |
Output is correct |
61 |
Correct |
229 ms |
23624 KB |
Output is correct |
62 |
Correct |
127 ms |
35188 KB |
Output is correct |
63 |
Correct |
133 ms |
35144 KB |
Output is correct |
64 |
Correct |
129 ms |
34888 KB |
Output is correct |
65 |
Correct |
135 ms |
35216 KB |
Output is correct |
66 |
Correct |
140 ms |
28584 KB |
Output is correct |
67 |
Correct |
169 ms |
28360 KB |
Output is correct |
68 |
Correct |
157 ms |
28608 KB |
Output is correct |
69 |
Correct |
161 ms |
28608 KB |
Output is correct |
70 |
Correct |
155 ms |
28608 KB |
Output is correct |
71 |
Correct |
148 ms |
28608 KB |
Output is correct |
72 |
Correct |
161 ms |
28608 KB |
Output is correct |
73 |
Correct |
180 ms |
27848 KB |
Output is correct |
74 |
Correct |
160 ms |
28608 KB |
Output is correct |
75 |
Correct |
157 ms |
28492 KB |
Output is correct |
76 |
Correct |
229 ms |
27208 KB |
Output is correct |
77 |
Correct |
227 ms |
25672 KB |
Output is correct |
78 |
Correct |
263 ms |
29916 KB |
Output is correct |
79 |
Correct |
279 ms |
28260 KB |
Output is correct |
80 |
Correct |
292 ms |
34016 KB |
Output is correct |
81 |
Correct |
275 ms |
31048 KB |
Output is correct |
82 |
Correct |
251 ms |
31124 KB |
Output is correct |
83 |
Correct |
237 ms |
28568 KB |
Output is correct |
84 |
Correct |
256 ms |
33076 KB |
Output is correct |
85 |
Correct |
264 ms |
31816 KB |
Output is correct |
86 |
Correct |
257 ms |
28232 KB |
Output is correct |
87 |
Correct |
254 ms |
29512 KB |
Output is correct |
88 |
Correct |
220 ms |
30280 KB |
Output is correct |
89 |
Correct |
208 ms |
27208 KB |
Output is correct |
90 |
Correct |
211 ms |
27164 KB |
Output is correct |
91 |
Correct |
243 ms |
29000 KB |
Output is correct |
92 |
Correct |
239 ms |
27976 KB |
Output is correct |
93 |
Correct |
221 ms |
27720 KB |
Output is correct |
94 |
Correct |
249 ms |
27316 KB |
Output is correct |
95 |
Correct |
253 ms |
28476 KB |
Output is correct |
96 |
Correct |
212 ms |
27208 KB |
Output is correct |
97 |
Correct |
232 ms |
29000 KB |
Output is correct |