// https://oj.uz/submission/954992
#include <bits/stdc++.h>
using namespace std;
const int N = 200008;
int n, k, cnt = 0, bl[N] /* block */, a[N], c[N], vis[N], g[N], ans = (int) 1e18, par[N], sz[N];
vector<int> adj[N], vt[N];
void dfs_sz(int u, int p) {
bl[u] = cnt;
sz[u] = 1;
for (int v : adj[u]) {
if (v == p || c[v]) continue;
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int find(int u, int p, int tot) {
int mx = -1, ret = -1;
for (int v : adj[u]) {
if (v == p || c[v]) continue;
ret = max(ret, find(v, u, tot));
mx = max(mx, sz[v]);
}
mx = max(mx, tot - sz[u]);
if (mx <= tot / 2) return u;
return ret;
}
void dfs_par(int u, int p) {
for (int v : adj[u]) {
if (v == p || c[v]) continue;
dfs_par(v, u);
par[v] = u;
}
}
void solve(int u) {
int cur = 0;
++cnt;
dfs_sz(u, -1);
int cent = find(u, -1, sz[u]);
c[cent] = 1;
dfs_par(cent, -1);
queue<int> q;
bool ok = 1;
g[a[cent]] = cnt;
q.push(a[cent]);
while (!q.empty()) {
auto node = q.front();
q.pop();
cur++;
for (int v : vt[node]) {
if (bl[v] != cnt) {
ok = 0;
break;
}
int x = v;
while (vis[x] != cnt) {
if (g[a[x]] != cnt) {
g[a[x]] = cnt;
q.push(a[x]);
}
vis[x] = cnt;
if (x == cent) break;
x = par[x];
}
}
if (!ok) break;
}
if (ok) ans = min(ans, cur);
for (int v : adj[cent]) if (!c[v]) solve(v);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
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++) {
g[i] = vis[i] = -1;
cin >> a[i];
vt[a[i]].push_back(i);
}
solve(1);
cout << ans - 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9776 KB |
Output is correct |
5 |
Correct |
4 ms |
9680 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9776 KB |
Output is correct |
5 |
Correct |
4 ms |
9680 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
5 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
10124 KB |
Output is correct |
16 |
Correct |
5 ms |
10076 KB |
Output is correct |
17 |
Correct |
4 ms |
10080 KB |
Output is correct |
18 |
Correct |
5 ms |
10184 KB |
Output is correct |
19 |
Correct |
5 ms |
10328 KB |
Output is correct |
20 |
Correct |
5 ms |
10072 KB |
Output is correct |
21 |
Correct |
5 ms |
10076 KB |
Output is correct |
22 |
Correct |
5 ms |
10076 KB |
Output is correct |
23 |
Correct |
6 ms |
10120 KB |
Output is correct |
24 |
Correct |
5 ms |
10076 KB |
Output is correct |
25 |
Correct |
5 ms |
10076 KB |
Output is correct |
26 |
Correct |
5 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
10080 KB |
Output is correct |
28 |
Correct |
5 ms |
10168 KB |
Output is correct |
29 |
Correct |
5 ms |
10080 KB |
Output is correct |
30 |
Correct |
5 ms |
10080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
46324 KB |
Output is correct |
2 |
Correct |
120 ms |
46980 KB |
Output is correct |
3 |
Correct |
273 ms |
46160 KB |
Output is correct |
4 |
Correct |
120 ms |
47132 KB |
Output is correct |
5 |
Correct |
273 ms |
42580 KB |
Output is correct |
6 |
Correct |
128 ms |
46960 KB |
Output is correct |
7 |
Correct |
277 ms |
43088 KB |
Output is correct |
8 |
Correct |
140 ms |
46744 KB |
Output is correct |
9 |
Correct |
322 ms |
41040 KB |
Output is correct |
10 |
Correct |
317 ms |
37972 KB |
Output is correct |
11 |
Correct |
337 ms |
41472 KB |
Output is correct |
12 |
Correct |
319 ms |
44624 KB |
Output is correct |
13 |
Correct |
314 ms |
37712 KB |
Output is correct |
14 |
Correct |
344 ms |
44992 KB |
Output is correct |
15 |
Correct |
336 ms |
44776 KB |
Output is correct |
16 |
Correct |
316 ms |
38736 KB |
Output is correct |
17 |
Correct |
343 ms |
39504 KB |
Output is correct |
18 |
Correct |
324 ms |
39504 KB |
Output is correct |
19 |
Correct |
318 ms |
43412 KB |
Output is correct |
20 |
Correct |
351 ms |
46020 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9820 KB |
Output is correct |
2 |
Correct |
5 ms |
9820 KB |
Output is correct |
3 |
Correct |
4 ms |
9820 KB |
Output is correct |
4 |
Correct |
4 ms |
9776 KB |
Output is correct |
5 |
Correct |
4 ms |
9680 KB |
Output is correct |
6 |
Correct |
4 ms |
9820 KB |
Output is correct |
7 |
Correct |
4 ms |
9816 KB |
Output is correct |
8 |
Correct |
4 ms |
9820 KB |
Output is correct |
9 |
Correct |
4 ms |
9820 KB |
Output is correct |
10 |
Correct |
4 ms |
9816 KB |
Output is correct |
11 |
Correct |
5 ms |
9820 KB |
Output is correct |
12 |
Correct |
5 ms |
9820 KB |
Output is correct |
13 |
Correct |
5 ms |
9820 KB |
Output is correct |
14 |
Correct |
5 ms |
9820 KB |
Output is correct |
15 |
Correct |
4 ms |
10124 KB |
Output is correct |
16 |
Correct |
5 ms |
10076 KB |
Output is correct |
17 |
Correct |
4 ms |
10080 KB |
Output is correct |
18 |
Correct |
5 ms |
10184 KB |
Output is correct |
19 |
Correct |
5 ms |
10328 KB |
Output is correct |
20 |
Correct |
5 ms |
10072 KB |
Output is correct |
21 |
Correct |
5 ms |
10076 KB |
Output is correct |
22 |
Correct |
5 ms |
10076 KB |
Output is correct |
23 |
Correct |
6 ms |
10120 KB |
Output is correct |
24 |
Correct |
5 ms |
10076 KB |
Output is correct |
25 |
Correct |
5 ms |
10076 KB |
Output is correct |
26 |
Correct |
5 ms |
10076 KB |
Output is correct |
27 |
Correct |
5 ms |
10080 KB |
Output is correct |
28 |
Correct |
5 ms |
10168 KB |
Output is correct |
29 |
Correct |
5 ms |
10080 KB |
Output is correct |
30 |
Correct |
5 ms |
10080 KB |
Output is correct |
31 |
Correct |
317 ms |
46324 KB |
Output is correct |
32 |
Correct |
120 ms |
46980 KB |
Output is correct |
33 |
Correct |
273 ms |
46160 KB |
Output is correct |
34 |
Correct |
120 ms |
47132 KB |
Output is correct |
35 |
Correct |
273 ms |
42580 KB |
Output is correct |
36 |
Correct |
128 ms |
46960 KB |
Output is correct |
37 |
Correct |
277 ms |
43088 KB |
Output is correct |
38 |
Correct |
140 ms |
46744 KB |
Output is correct |
39 |
Correct |
322 ms |
41040 KB |
Output is correct |
40 |
Correct |
317 ms |
37972 KB |
Output is correct |
41 |
Correct |
337 ms |
41472 KB |
Output is correct |
42 |
Correct |
319 ms |
44624 KB |
Output is correct |
43 |
Correct |
314 ms |
37712 KB |
Output is correct |
44 |
Correct |
344 ms |
44992 KB |
Output is correct |
45 |
Correct |
336 ms |
44776 KB |
Output is correct |
46 |
Correct |
316 ms |
38736 KB |
Output is correct |
47 |
Correct |
343 ms |
39504 KB |
Output is correct |
48 |
Correct |
324 ms |
39504 KB |
Output is correct |
49 |
Correct |
318 ms |
43412 KB |
Output is correct |
50 |
Correct |
351 ms |
46020 KB |
Output is correct |
51 |
Correct |
4 ms |
9820 KB |
Output is correct |
52 |
Correct |
190 ms |
26884 KB |
Output is correct |
53 |
Correct |
212 ms |
26960 KB |
Output is correct |
54 |
Correct |
212 ms |
26780 KB |
Output is correct |
55 |
Correct |
205 ms |
26704 KB |
Output is correct |
56 |
Correct |
214 ms |
26988 KB |
Output is correct |
57 |
Correct |
205 ms |
26740 KB |
Output is correct |
58 |
Correct |
215 ms |
31060 KB |
Output is correct |
59 |
Correct |
198 ms |
31488 KB |
Output is correct |
60 |
Correct |
243 ms |
30308 KB |
Output is correct |
61 |
Correct |
242 ms |
30048 KB |
Output is correct |
62 |
Correct |
125 ms |
46932 KB |
Output is correct |
63 |
Correct |
123 ms |
46932 KB |
Output is correct |
64 |
Correct |
119 ms |
46928 KB |
Output is correct |
65 |
Correct |
124 ms |
46928 KB |
Output is correct |
66 |
Correct |
177 ms |
37068 KB |
Output is correct |
67 |
Correct |
174 ms |
36812 KB |
Output is correct |
68 |
Correct |
169 ms |
36936 KB |
Output is correct |
69 |
Correct |
168 ms |
37068 KB |
Output is correct |
70 |
Correct |
177 ms |
36988 KB |
Output is correct |
71 |
Correct |
158 ms |
36808 KB |
Output is correct |
72 |
Correct |
172 ms |
36908 KB |
Output is correct |
73 |
Correct |
165 ms |
36040 KB |
Output is correct |
74 |
Correct |
168 ms |
36924 KB |
Output is correct |
75 |
Correct |
172 ms |
37064 KB |
Output is correct |
76 |
Correct |
245 ms |
35080 KB |
Output is correct |
77 |
Correct |
236 ms |
32852 KB |
Output is correct |
78 |
Correct |
322 ms |
39456 KB |
Output is correct |
79 |
Correct |
325 ms |
37072 KB |
Output is correct |
80 |
Correct |
311 ms |
45392 KB |
Output is correct |
81 |
Correct |
349 ms |
41348 KB |
Output is correct |
82 |
Correct |
322 ms |
41540 KB |
Output is correct |
83 |
Correct |
316 ms |
37460 KB |
Output is correct |
84 |
Correct |
362 ms |
44368 KB |
Output is correct |
85 |
Correct |
331 ms |
42576 KB |
Output is correct |
86 |
Correct |
318 ms |
36880 KB |
Output is correct |
87 |
Correct |
325 ms |
38992 KB |
Output is correct |
88 |
Correct |
291 ms |
40272 KB |
Output is correct |
89 |
Correct |
271 ms |
35724 KB |
Output is correct |
90 |
Correct |
272 ms |
35204 KB |
Output is correct |
91 |
Correct |
268 ms |
38224 KB |
Output is correct |
92 |
Correct |
285 ms |
36672 KB |
Output is correct |
93 |
Correct |
262 ms |
36248 KB |
Output is correct |
94 |
Correct |
275 ms |
35364 KB |
Output is correct |
95 |
Correct |
270 ms |
37284 KB |
Output is correct |
96 |
Correct |
291 ms |
35728 KB |
Output is correct |
97 |
Correct |
271 ms |
38056 KB |
Output is correct |