#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
3 ms |
12880 KB |
Output is correct |
4 |
Correct |
3 ms |
12880 KB |
Output is correct |
5 |
Correct |
3 ms |
13136 KB |
Output is correct |
6 |
Correct |
3 ms |
12956 KB |
Output is correct |
7 |
Correct |
3 ms |
12880 KB |
Output is correct |
8 |
Correct |
4 ms |
12952 KB |
Output is correct |
9 |
Correct |
3 ms |
12948 KB |
Output is correct |
10 |
Correct |
4 ms |
12952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
3 ms |
12880 KB |
Output is correct |
4 |
Correct |
3 ms |
12880 KB |
Output is correct |
5 |
Correct |
3 ms |
13136 KB |
Output is correct |
6 |
Correct |
3 ms |
12956 KB |
Output is correct |
7 |
Correct |
3 ms |
12880 KB |
Output is correct |
8 |
Correct |
4 ms |
12952 KB |
Output is correct |
9 |
Correct |
3 ms |
12948 KB |
Output is correct |
10 |
Correct |
4 ms |
12952 KB |
Output is correct |
11 |
Correct |
5 ms |
13004 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
4 ms |
12880 KB |
Output is correct |
14 |
Correct |
4 ms |
12880 KB |
Output is correct |
15 |
Correct |
4 ms |
12880 KB |
Output is correct |
16 |
Correct |
5 ms |
12880 KB |
Output is correct |
17 |
Correct |
3 ms |
13064 KB |
Output is correct |
18 |
Correct |
4 ms |
12880 KB |
Output is correct |
19 |
Correct |
3 ms |
13136 KB |
Output is correct |
20 |
Correct |
5 ms |
13036 KB |
Output is correct |
21 |
Correct |
5 ms |
13136 KB |
Output is correct |
22 |
Correct |
3 ms |
13136 KB |
Output is correct |
23 |
Correct |
5 ms |
13136 KB |
Output is correct |
24 |
Correct |
4 ms |
12880 KB |
Output is correct |
25 |
Correct |
4 ms |
13152 KB |
Output is correct |
26 |
Correct |
4 ms |
13136 KB |
Output is correct |
27 |
Correct |
5 ms |
13136 KB |
Output is correct |
28 |
Correct |
4 ms |
13136 KB |
Output is correct |
29 |
Correct |
4 ms |
12880 KB |
Output is correct |
30 |
Correct |
4 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
38528 KB |
Output is correct |
2 |
Correct |
153 ms |
38728 KB |
Output is correct |
3 |
Correct |
278 ms |
38216 KB |
Output is correct |
4 |
Correct |
160 ms |
38728 KB |
Output is correct |
5 |
Correct |
266 ms |
35880 KB |
Output is correct |
6 |
Correct |
135 ms |
38920 KB |
Output is correct |
7 |
Correct |
240 ms |
36172 KB |
Output is correct |
8 |
Correct |
149 ms |
38516 KB |
Output is correct |
9 |
Correct |
259 ms |
34632 KB |
Output is correct |
10 |
Correct |
262 ms |
32584 KB |
Output is correct |
11 |
Correct |
264 ms |
34888 KB |
Output is correct |
12 |
Correct |
260 ms |
37156 KB |
Output is correct |
13 |
Correct |
257 ms |
32344 KB |
Output is correct |
14 |
Correct |
276 ms |
37192 KB |
Output is correct |
15 |
Correct |
269 ms |
37024 KB |
Output is correct |
16 |
Correct |
250 ms |
33148 KB |
Output is correct |
17 |
Correct |
248 ms |
33404 KB |
Output is correct |
18 |
Correct |
266 ms |
33608 KB |
Output is correct |
19 |
Correct |
253 ms |
36224 KB |
Output is correct |
20 |
Correct |
259 ms |
37960 KB |
Output is correct |
21 |
Correct |
2 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12880 KB |
Output is correct |
2 |
Correct |
3 ms |
12880 KB |
Output is correct |
3 |
Correct |
3 ms |
12880 KB |
Output is correct |
4 |
Correct |
3 ms |
12880 KB |
Output is correct |
5 |
Correct |
3 ms |
13136 KB |
Output is correct |
6 |
Correct |
3 ms |
12956 KB |
Output is correct |
7 |
Correct |
3 ms |
12880 KB |
Output is correct |
8 |
Correct |
4 ms |
12952 KB |
Output is correct |
9 |
Correct |
3 ms |
12948 KB |
Output is correct |
10 |
Correct |
4 ms |
12952 KB |
Output is correct |
11 |
Correct |
5 ms |
13004 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
4 ms |
12880 KB |
Output is correct |
14 |
Correct |
4 ms |
12880 KB |
Output is correct |
15 |
Correct |
4 ms |
12880 KB |
Output is correct |
16 |
Correct |
5 ms |
12880 KB |
Output is correct |
17 |
Correct |
3 ms |
13064 KB |
Output is correct |
18 |
Correct |
4 ms |
12880 KB |
Output is correct |
19 |
Correct |
3 ms |
13136 KB |
Output is correct |
20 |
Correct |
5 ms |
13036 KB |
Output is correct |
21 |
Correct |
5 ms |
13136 KB |
Output is correct |
22 |
Correct |
3 ms |
13136 KB |
Output is correct |
23 |
Correct |
5 ms |
13136 KB |
Output is correct |
24 |
Correct |
4 ms |
12880 KB |
Output is correct |
25 |
Correct |
4 ms |
13152 KB |
Output is correct |
26 |
Correct |
4 ms |
13136 KB |
Output is correct |
27 |
Correct |
5 ms |
13136 KB |
Output is correct |
28 |
Correct |
4 ms |
13136 KB |
Output is correct |
29 |
Correct |
4 ms |
12880 KB |
Output is correct |
30 |
Correct |
4 ms |
12880 KB |
Output is correct |
31 |
Correct |
285 ms |
38528 KB |
Output is correct |
32 |
Correct |
153 ms |
38728 KB |
Output is correct |
33 |
Correct |
278 ms |
38216 KB |
Output is correct |
34 |
Correct |
160 ms |
38728 KB |
Output is correct |
35 |
Correct |
266 ms |
35880 KB |
Output is correct |
36 |
Correct |
135 ms |
38920 KB |
Output is correct |
37 |
Correct |
240 ms |
36172 KB |
Output is correct |
38 |
Correct |
149 ms |
38516 KB |
Output is correct |
39 |
Correct |
259 ms |
34632 KB |
Output is correct |
40 |
Correct |
262 ms |
32584 KB |
Output is correct |
41 |
Correct |
264 ms |
34888 KB |
Output is correct |
42 |
Correct |
260 ms |
37156 KB |
Output is correct |
43 |
Correct |
257 ms |
32344 KB |
Output is correct |
44 |
Correct |
276 ms |
37192 KB |
Output is correct |
45 |
Correct |
269 ms |
37024 KB |
Output is correct |
46 |
Correct |
250 ms |
33148 KB |
Output is correct |
47 |
Correct |
248 ms |
33404 KB |
Output is correct |
48 |
Correct |
266 ms |
33608 KB |
Output is correct |
49 |
Correct |
253 ms |
36224 KB |
Output is correct |
50 |
Correct |
259 ms |
37960 KB |
Output is correct |
51 |
Correct |
2 ms |
12880 KB |
Output is correct |
52 |
Correct |
148 ms |
25416 KB |
Output is correct |
53 |
Correct |
164 ms |
25236 KB |
Output is correct |
54 |
Correct |
170 ms |
25416 KB |
Output is correct |
55 |
Correct |
175 ms |
25356 KB |
Output is correct |
56 |
Correct |
181 ms |
25424 KB |
Output is correct |
57 |
Correct |
217 ms |
25188 KB |
Output is correct |
58 |
Correct |
162 ms |
28000 KB |
Output is correct |
59 |
Correct |
164 ms |
28240 KB |
Output is correct |
60 |
Correct |
195 ms |
27568 KB |
Output is correct |
61 |
Correct |
193 ms |
27464 KB |
Output is correct |
62 |
Correct |
140 ms |
38872 KB |
Output is correct |
63 |
Correct |
137 ms |
38744 KB |
Output is correct |
64 |
Correct |
149 ms |
38728 KB |
Output is correct |
65 |
Correct |
128 ms |
38704 KB |
Output is correct |
66 |
Correct |
148 ms |
32192 KB |
Output is correct |
67 |
Correct |
158 ms |
32104 KB |
Output is correct |
68 |
Correct |
150 ms |
32120 KB |
Output is correct |
69 |
Correct |
159 ms |
32276 KB |
Output is correct |
70 |
Correct |
140 ms |
32196 KB |
Output is correct |
71 |
Correct |
190 ms |
32192 KB |
Output is correct |
72 |
Correct |
148 ms |
32192 KB |
Output is correct |
73 |
Correct |
152 ms |
31424 KB |
Output is correct |
74 |
Correct |
143 ms |
32336 KB |
Output is correct |
75 |
Correct |
160 ms |
32228 KB |
Output is correct |
76 |
Correct |
214 ms |
30792 KB |
Output is correct |
77 |
Correct |
209 ms |
29256 KB |
Output is correct |
78 |
Correct |
258 ms |
33528 KB |
Output is correct |
79 |
Correct |
250 ms |
31820 KB |
Output is correct |
80 |
Correct |
251 ms |
37452 KB |
Output is correct |
81 |
Correct |
255 ms |
34632 KB |
Output is correct |
82 |
Correct |
262 ms |
34888 KB |
Output is correct |
83 |
Correct |
246 ms |
32072 KB |
Output is correct |
84 |
Correct |
258 ms |
36680 KB |
Output is correct |
85 |
Correct |
266 ms |
35688 KB |
Output is correct |
86 |
Correct |
250 ms |
31896 KB |
Output is correct |
87 |
Correct |
268 ms |
33172 KB |
Output is correct |
88 |
Correct |
240 ms |
34120 KB |
Output is correct |
89 |
Correct |
214 ms |
30964 KB |
Output is correct |
90 |
Correct |
220 ms |
30792 KB |
Output is correct |
91 |
Correct |
246 ms |
32764 KB |
Output is correct |
92 |
Correct |
219 ms |
31560 KB |
Output is correct |
93 |
Correct |
239 ms |
31540 KB |
Output is correct |
94 |
Correct |
225 ms |
30792 KB |
Output is correct |
95 |
Correct |
232 ms |
32176 KB |
Output is correct |
96 |
Correct |
222 ms |
30848 KB |
Output is correct |
97 |
Correct |
234 ms |
32584 KB |
Output is correct |