// https://oj.uz/problem/view/JOI20_capital_city
#include <bits/stdc++.h>
using namespace std;
namespace std {
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
/* Example
auto fun = y_combinator([&](auto self, int x) -> void {
self(x + 1);
});
*/
} // namespace std
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
vector<int> c(n);
for (int i = 0; i < n; i++) cin >> c[i], c[i]--;
vector<int> d(n);
vector<vector<int>> up(__lg(n) + 1, vector<int>(n));
vector<vector<int>> rmq(__lg(n) + 1, vector<int>(n));
vector<vector<int>> g(k);
vector<vector<int>> rg(k);
auto dfs = y_combinator([&](auto self, int u, int p) -> void {
up[0][u] = p;
rmq[0][u] = c[u];
for (int i = 1; i <= __lg(n); i++) {
up[i][u] = up[i - 1][up[i - 1][u]];
if (up[i - 1][u] == u) continue;
rmq[i][u] = g.size();
g.emplace_back(vector<int>({rmq[i - 1][u], rmq[i - 1][up[i - 1][u]]}));
}
for (int v : adj[u]) {
if (v == p) continue;
d[v] = d[u] + 1;
self(v, u);
}
});
auto lca = [&](int group_id, int x, int y) {
if (d[x] < d[y]) swap(x, y);
for (int i = __lg(n); i >= 0; i--) {
if (d[up[i][x]] >= d[y]) {
g[group_id].emplace_back(rmq[i][x]);
x = up[i][x];
}
}
if (x == y) {
g[group_id].emplace_back(rmq[0][x]);
return x;
}
for (int i = __lg(n); i >= 0; i--) {
if (up[i][x] != up[i][y]) {
g[group_id].emplace_back(rmq[i][x]);
g[group_id].emplace_back(rmq[i][y]);
x = up[i][x], y = up[i][y];
}
}
g[group_id].emplace_back(rmq[0][x]);
g[group_id].emplace_back(rmq[1][y]);
return up[0][x];
};
vector<vector<int>> group(k);
for (int i = 0; i < n; i++) group[c[i]].emplace_back(i);
dfs(0, 0);
for (int i = 0; i < k; i++) {
int top = group[i][0];
for (int j : group[i]) top = lca(i, top, j);
}
int N = g.size();
vector<int> low(N), num(N);
int timer = 0;
stack<int> st;
int M = 0;
vector<int> comp(N);
vector<int> cnt;
auto dfs2 = y_combinator([&](auto self, int u) -> void {
low[u] = num[u] = ++timer;
st.emplace(u);
for (int v : g[u]) {
if (num[v]) {
low[u] = min(low[u], num[v]);
} else {
self(v);
low[u] = min(low[u], low[v]);
}
}
if (low[u] == num[u]) {
int v = -1;
cnt.emplace_back(0);
do {
v = st.top();
comp[v] = M;
st.pop();
num[v] = low[v] = N + 1;
cnt[M] += v < k;
} while (v != u);
M++;
}
});
for (int i = 0; i < N; i++) {
if (!num[i]) dfs2(i);
}
for (int i = 0; i < N; i++) {
for (int j : g[i]) {
if (comp[i] == comp[j]) continue;
cnt[comp[i]] = n;
}
}
int res = k;
for (int i = 0; i < k; i++) {
if (cnt[comp[i]]) res = min(res, cnt[comp[i]] - 1);
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
500 KB |
Output is correct |
11 |
Correct |
4 ms |
2340 KB |
Output is correct |
12 |
Correct |
3 ms |
2344 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
4 ms |
2344 KB |
Output is correct |
15 |
Correct |
3 ms |
2220 KB |
Output is correct |
16 |
Correct |
3 ms |
2408 KB |
Output is correct |
17 |
Correct |
3 ms |
2332 KB |
Output is correct |
18 |
Correct |
3 ms |
2336 KB |
Output is correct |
19 |
Correct |
3 ms |
2584 KB |
Output is correct |
20 |
Correct |
3 ms |
2332 KB |
Output is correct |
21 |
Correct |
3 ms |
2336 KB |
Output is correct |
22 |
Correct |
3 ms |
2600 KB |
Output is correct |
23 |
Correct |
3 ms |
2616 KB |
Output is correct |
24 |
Correct |
3 ms |
2344 KB |
Output is correct |
25 |
Correct |
3 ms |
2564 KB |
Output is correct |
26 |
Correct |
4 ms |
2332 KB |
Output is correct |
27 |
Correct |
4 ms |
2304 KB |
Output is correct |
28 |
Correct |
4 ms |
2328 KB |
Output is correct |
29 |
Correct |
3 ms |
2316 KB |
Output is correct |
30 |
Correct |
3 ms |
2584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1588 ms |
401144 KB |
Output is correct |
2 |
Correct |
672 ms |
330884 KB |
Output is correct |
3 |
Correct |
1448 ms |
348864 KB |
Output is correct |
4 |
Correct |
685 ms |
330464 KB |
Output is correct |
5 |
Correct |
1417 ms |
393872 KB |
Output is correct |
6 |
Correct |
664 ms |
330444 KB |
Output is correct |
7 |
Correct |
1435 ms |
375356 KB |
Output is correct |
8 |
Correct |
630 ms |
332848 KB |
Output is correct |
9 |
Correct |
1452 ms |
316280 KB |
Output is correct |
10 |
Correct |
1395 ms |
311092 KB |
Output is correct |
11 |
Correct |
1368 ms |
315308 KB |
Output is correct |
12 |
Correct |
1408 ms |
320052 KB |
Output is correct |
13 |
Correct |
1433 ms |
312196 KB |
Output is correct |
14 |
Correct |
1427 ms |
318812 KB |
Output is correct |
15 |
Correct |
1402 ms |
318848 KB |
Output is correct |
16 |
Correct |
1389 ms |
310440 KB |
Output is correct |
17 |
Correct |
1383 ms |
312912 KB |
Output is correct |
18 |
Correct |
1341 ms |
312740 KB |
Output is correct |
19 |
Correct |
1423 ms |
317124 KB |
Output is correct |
20 |
Correct |
1412 ms |
321232 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
500 KB |
Output is correct |
11 |
Correct |
4 ms |
2340 KB |
Output is correct |
12 |
Correct |
3 ms |
2344 KB |
Output is correct |
13 |
Correct |
3 ms |
2380 KB |
Output is correct |
14 |
Correct |
4 ms |
2344 KB |
Output is correct |
15 |
Correct |
3 ms |
2220 KB |
Output is correct |
16 |
Correct |
3 ms |
2408 KB |
Output is correct |
17 |
Correct |
3 ms |
2332 KB |
Output is correct |
18 |
Correct |
3 ms |
2336 KB |
Output is correct |
19 |
Correct |
3 ms |
2584 KB |
Output is correct |
20 |
Correct |
3 ms |
2332 KB |
Output is correct |
21 |
Correct |
3 ms |
2336 KB |
Output is correct |
22 |
Correct |
3 ms |
2600 KB |
Output is correct |
23 |
Correct |
3 ms |
2616 KB |
Output is correct |
24 |
Correct |
3 ms |
2344 KB |
Output is correct |
25 |
Correct |
3 ms |
2564 KB |
Output is correct |
26 |
Correct |
4 ms |
2332 KB |
Output is correct |
27 |
Correct |
4 ms |
2304 KB |
Output is correct |
28 |
Correct |
4 ms |
2328 KB |
Output is correct |
29 |
Correct |
3 ms |
2316 KB |
Output is correct |
30 |
Correct |
3 ms |
2584 KB |
Output is correct |
31 |
Correct |
1588 ms |
401144 KB |
Output is correct |
32 |
Correct |
672 ms |
330884 KB |
Output is correct |
33 |
Correct |
1448 ms |
348864 KB |
Output is correct |
34 |
Correct |
685 ms |
330464 KB |
Output is correct |
35 |
Correct |
1417 ms |
393872 KB |
Output is correct |
36 |
Correct |
664 ms |
330444 KB |
Output is correct |
37 |
Correct |
1435 ms |
375356 KB |
Output is correct |
38 |
Correct |
630 ms |
332848 KB |
Output is correct |
39 |
Correct |
1452 ms |
316280 KB |
Output is correct |
40 |
Correct |
1395 ms |
311092 KB |
Output is correct |
41 |
Correct |
1368 ms |
315308 KB |
Output is correct |
42 |
Correct |
1408 ms |
320052 KB |
Output is correct |
43 |
Correct |
1433 ms |
312196 KB |
Output is correct |
44 |
Correct |
1427 ms |
318812 KB |
Output is correct |
45 |
Correct |
1402 ms |
318848 KB |
Output is correct |
46 |
Correct |
1389 ms |
310440 KB |
Output is correct |
47 |
Correct |
1383 ms |
312912 KB |
Output is correct |
48 |
Correct |
1341 ms |
312740 KB |
Output is correct |
49 |
Correct |
1423 ms |
317124 KB |
Output is correct |
50 |
Correct |
1412 ms |
321232 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
1100 ms |
316732 KB |
Output is correct |
53 |
Correct |
1030 ms |
312524 KB |
Output is correct |
54 |
Correct |
1095 ms |
311844 KB |
Output is correct |
55 |
Correct |
1062 ms |
309900 KB |
Output is correct |
56 |
Correct |
1081 ms |
315308 KB |
Output is correct |
57 |
Correct |
1002 ms |
314260 KB |
Output is correct |
58 |
Correct |
1095 ms |
326588 KB |
Output is correct |
59 |
Correct |
1129 ms |
327424 KB |
Output is correct |
60 |
Correct |
1231 ms |
313116 KB |
Output is correct |
61 |
Correct |
1231 ms |
313192 KB |
Output is correct |
62 |
Correct |
674 ms |
330424 KB |
Output is correct |
63 |
Correct |
651 ms |
330428 KB |
Output is correct |
64 |
Correct |
684 ms |
329688 KB |
Output is correct |
65 |
Correct |
673 ms |
331048 KB |
Output is correct |
66 |
Correct |
1171 ms |
311720 KB |
Output is correct |
67 |
Correct |
1196 ms |
318464 KB |
Output is correct |
68 |
Correct |
1135 ms |
313700 KB |
Output is correct |
69 |
Correct |
1160 ms |
313860 KB |
Output is correct |
70 |
Correct |
1174 ms |
312872 KB |
Output is correct |
71 |
Correct |
1183 ms |
312488 KB |
Output is correct |
72 |
Correct |
1184 ms |
313056 KB |
Output is correct |
73 |
Correct |
1174 ms |
311328 KB |
Output is correct |
74 |
Correct |
1178 ms |
313468 KB |
Output is correct |
75 |
Correct |
1167 ms |
312524 KB |
Output is correct |
76 |
Correct |
1075 ms |
326876 KB |
Output is correct |
77 |
Correct |
1090 ms |
322148 KB |
Output is correct |
78 |
Correct |
1446 ms |
312020 KB |
Output is correct |
79 |
Correct |
1383 ms |
310240 KB |
Output is correct |
80 |
Correct |
1394 ms |
320264 KB |
Output is correct |
81 |
Correct |
1383 ms |
316328 KB |
Output is correct |
82 |
Correct |
1408 ms |
316404 KB |
Output is correct |
83 |
Correct |
1508 ms |
310708 KB |
Output is correct |
84 |
Correct |
1447 ms |
321608 KB |
Output is correct |
85 |
Correct |
1442 ms |
316964 KB |
Output is correct |
86 |
Correct |
1478 ms |
311256 KB |
Output is correct |
87 |
Correct |
1401 ms |
313108 KB |
Output is correct |
88 |
Correct |
1362 ms |
315716 KB |
Output is correct |
89 |
Correct |
1386 ms |
308816 KB |
Output is correct |
90 |
Correct |
1334 ms |
307584 KB |
Output is correct |
91 |
Correct |
1311 ms |
312432 KB |
Output is correct |
92 |
Correct |
1349 ms |
310860 KB |
Output is correct |
93 |
Correct |
1349 ms |
310480 KB |
Output is correct |
94 |
Correct |
1348 ms |
308768 KB |
Output is correct |
95 |
Correct |
1379 ms |
310068 KB |
Output is correct |
96 |
Correct |
1337 ms |
308236 KB |
Output is correct |
97 |
Correct |
1386 ms |
312184 KB |
Output is correct |