#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <stdlib.h>
#include <map>
#include <random>
#include <cstring>
#include <functional>
#include <iomanip>
#include <cassert>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <array>
using namespace std;
const int inf = 1e9;
const int maxn = 200000;
int par[maxn];
int tin[maxn];
int tout[maxn];
int up[20][maxn];
int upp[20][maxn];
int d[maxn];
vector<int> gp[maxn * 21];
int timer = 0;
vector<int> c;
int id = maxn;
void dfs(int v, vector<vector<int>> &g, int p) {
tin[v] = timer++;
up[0][v] = p;
gp[id].push_back(c[v]);
upp[0][v] = id++;
for (int i = 1; i < 20; ++i) {
up[i][v] = up[i - 1][up[i - 1][v]];
gp[id].push_back(upp[i - 1][up[i - 1][v]]);
gp[id].push_back(upp[i - 1][v]);
upp[i][v] = id++;
}
for (int u : g[v]) {
if (u != p) {
par[u] = v;
d[u] = d[v] + 1;
dfs(u, g, v);
}
}
tout[v] = timer++;
}
void dfsrev(int v, vector<vector<int>> &g, int p) {
gp[c[v]].push_back(id);
upp[0][v] = id++;
for (int i = 1; i < 20; ++i) {
gp[upp[i - 1][up[i - 1][v]]].push_back(id);
gp[upp[i - 1][v]].push_back(id);
upp[i][v] = id++;
}
for (int u : g[v]) {
if (u != p) {
dfsrev(u, g, v);
}
}
}
bool isa(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v) {
if (isa(u, v)) return u;
if (isa(v, u)) return v;
for (int i = 19; i >= 0; --i) {
if (!isa(u, v)) {
u = up[i][u];
}
}
return up[0][u];
}
vector<int> ord;
void dfs(int v, vector<int> *g, vector<bool> &used) {
used[v] = true;
for (int u : g[v]) {
if (!used[u]) {
dfs(u, g, used);
}
}
ord.push_back(v);
}
void dfs(int v, vector<int> *g, vector<int> &color, int cur) {
color[v] = cur;
for (int u : g[v]) {
if (!color[u]) {
dfs(u, g, color, cur);
}
}
}
void solve() {
int n, k;
cin >> n >> k;
vector<vector<int>> g(n);
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
fill(par, par + maxn, -1);
c.resize(n);
for (int& i : c) cin >> i;
for (int& i : c) --i;
dfs(0, g, 0);
vector<vector<int>> groups(k);
for (int i = 0; i < n; ++i) {
groups[c[i]].push_back(i);
}
auto add_edges = [&] (int vertex, int v, int k) {
++k;
for (int i = 19; i >= 0; --i) {
if (k >= (1 << i)) {
gp[vertex].push_back(upp[i][v]);
v = up[i][v];
k -= (1 << i);
}
}
};
for (vector<int> g : groups) {
vector<pair<int, int>> x;
for (int i : g) {
x.push_back({ tin[i], i });
}
sort(x.begin(), x.end());
vector<pair<int, int>> y = x;
for (int i = 0; i + 1 < x.size(); ++i) {
y.push_back({ tin[lca(x[i].second, x[i + 1].second)], lca(x[i].second, x[i + 1].second) });
}
sort(y.begin(), y.end());
vector<int> st;
for (auto [x, v] : y) {
while (!st.empty() && !isa(st.back(), v)) st.pop_back();
if (!st.empty()) {
int k = d[v] - d[st.back()];
add_edges(c[v], v, k);
}
st.push_back(v);
}
}
vector<bool> used(id, false);
for (int i = 0; i < id; ++i) {
if (!used[i]) dfs(i, gp, used);
}
reverse(ord.begin(), ord.end());
vector<int> color(id, 0);
for (int i = 0; i < id; ++i) {
gp[i].clear();
}
id = maxn;
dfsrev(0, g, 0);
auto add_rev_edges = [&] (int vertex, int v, int k) {
++k;
for (int i = 19; i >= 0; --i) {
if (k >= (1 << i)) {
gp[upp[i][v]].push_back(vertex);
v = up[i][v];
k -= (1 << i);
}
}
};
for (vector<int> g : groups) {
vector<pair<int, int>> x;
for (int i : g) {
x.push_back({ tin[i], i });
}
sort(x.begin(), x.end());
vector<pair<int, int>> y = x;
for (int i = 0; i + 1 < x.size(); ++i) {
y.push_back({ tin[lca(x[i].second, x[i + 1].second)], lca(x[i].second, x[i + 1].second) });
}
sort(y.begin(), y.end());
vector<int> st;
for (auto [x, v] : y) {
while (!st.empty() && !isa(st.back(), v)) st.pop_back();
if (!st.empty()) {
int k = d[v] - d[st.back()];
add_rev_edges(c[v], v, k);
}
st.push_back(v);
}
}
int cur = 1;
for (int i : ord) {
if (!color[i]) {
dfs(i, gp, color, cur++);
}
}
vector<int> sz(k, 1);
sz.resize(id);
vector<int> sum(id, 0);
for (int i = 0; i < k; ++i) {
sum[color[i] - 1] += sz[i];
}
int res = inf;
--cur;
vector<bool> ok(cur, true);
for (int i = 0; i < id; ++i) {
for (int j : gp[i]) {
if (color[j] != color[i]) {
ok[color[j] - 1] = false;
}
}
}
for (int i = 0; i < cur; ++i) {
if (ok[i] && sum[i]) {
res = min(res, sum[i]);
}
}
cout << res - 1;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
}
/*
12 4
7 9
1 3
4 6
2 4
10 12
1 2
2 10
11 1
2 8
5 3
6 7
3
1 1 2 4 3 3 2 2 3 4 4
6 3
2 1
3 5
6 2
3 4
2 3
1
3 1 2 3 2
*/
Compilation message
capital_city.cpp: In function 'void solve()':
capital_city.cpp:142:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for (int i = 0; i + 1 < x.size(); ++i) {
| ~~~~~~^~~~~~~~~~
capital_city.cpp:184:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
184 | for (int i = 0; i + 1 < x.size(); ++i) {
| ~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
103232 KB |
Output is correct |
2 |
Correct |
50 ms |
103176 KB |
Output is correct |
3 |
Correct |
51 ms |
103224 KB |
Output is correct |
4 |
Correct |
52 ms |
103228 KB |
Output is correct |
5 |
Correct |
51 ms |
103188 KB |
Output is correct |
6 |
Correct |
50 ms |
103240 KB |
Output is correct |
7 |
Correct |
50 ms |
103224 KB |
Output is correct |
8 |
Correct |
52 ms |
103252 KB |
Output is correct |
9 |
Correct |
50 ms |
103232 KB |
Output is correct |
10 |
Correct |
50 ms |
103240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
103232 KB |
Output is correct |
2 |
Correct |
50 ms |
103176 KB |
Output is correct |
3 |
Correct |
51 ms |
103224 KB |
Output is correct |
4 |
Correct |
52 ms |
103228 KB |
Output is correct |
5 |
Correct |
51 ms |
103188 KB |
Output is correct |
6 |
Correct |
50 ms |
103240 KB |
Output is correct |
7 |
Correct |
50 ms |
103224 KB |
Output is correct |
8 |
Correct |
52 ms |
103252 KB |
Output is correct |
9 |
Correct |
50 ms |
103232 KB |
Output is correct |
10 |
Correct |
50 ms |
103240 KB |
Output is correct |
11 |
Correct |
59 ms |
105784 KB |
Output is correct |
12 |
Correct |
58 ms |
105692 KB |
Output is correct |
13 |
Correct |
61 ms |
105796 KB |
Output is correct |
14 |
Correct |
62 ms |
105676 KB |
Output is correct |
15 |
Correct |
58 ms |
105780 KB |
Output is correct |
16 |
Correct |
58 ms |
105768 KB |
Output is correct |
17 |
Correct |
59 ms |
105784 KB |
Output is correct |
18 |
Correct |
58 ms |
105764 KB |
Output is correct |
19 |
Correct |
58 ms |
105816 KB |
Output is correct |
20 |
Correct |
61 ms |
105768 KB |
Output is correct |
21 |
Correct |
59 ms |
105780 KB |
Output is correct |
22 |
Correct |
60 ms |
105872 KB |
Output is correct |
23 |
Correct |
59 ms |
105784 KB |
Output is correct |
24 |
Correct |
59 ms |
105764 KB |
Output is correct |
25 |
Correct |
61 ms |
105704 KB |
Output is correct |
26 |
Correct |
60 ms |
105700 KB |
Output is correct |
27 |
Correct |
59 ms |
105744 KB |
Output is correct |
28 |
Correct |
64 ms |
105804 KB |
Output is correct |
29 |
Correct |
60 ms |
105660 KB |
Output is correct |
30 |
Correct |
60 ms |
105672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2655 ms |
371736 KB |
Output is correct |
2 |
Correct |
1408 ms |
375492 KB |
Output is correct |
3 |
Correct |
2680 ms |
375032 KB |
Output is correct |
4 |
Correct |
1415 ms |
375468 KB |
Output is correct |
5 |
Correct |
2501 ms |
372944 KB |
Output is correct |
6 |
Correct |
1393 ms |
375604 KB |
Output is correct |
7 |
Correct |
2485 ms |
371112 KB |
Output is correct |
8 |
Correct |
1343 ms |
374264 KB |
Output is correct |
9 |
Correct |
1958 ms |
364904 KB |
Output is correct |
10 |
Correct |
1937 ms |
362260 KB |
Output is correct |
11 |
Correct |
1944 ms |
364992 KB |
Output is correct |
12 |
Correct |
1981 ms |
368612 KB |
Output is correct |
13 |
Correct |
1973 ms |
362120 KB |
Output is correct |
14 |
Correct |
1958 ms |
368792 KB |
Output is correct |
15 |
Correct |
2083 ms |
368556 KB |
Output is correct |
16 |
Correct |
1978 ms |
362956 KB |
Output is correct |
17 |
Correct |
1981 ms |
363740 KB |
Output is correct |
18 |
Correct |
1954 ms |
363808 KB |
Output is correct |
19 |
Correct |
2075 ms |
366904 KB |
Output is correct |
20 |
Correct |
2000 ms |
369200 KB |
Output is correct |
21 |
Correct |
53 ms |
103236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
103232 KB |
Output is correct |
2 |
Correct |
50 ms |
103176 KB |
Output is correct |
3 |
Correct |
51 ms |
103224 KB |
Output is correct |
4 |
Correct |
52 ms |
103228 KB |
Output is correct |
5 |
Correct |
51 ms |
103188 KB |
Output is correct |
6 |
Correct |
50 ms |
103240 KB |
Output is correct |
7 |
Correct |
50 ms |
103224 KB |
Output is correct |
8 |
Correct |
52 ms |
103252 KB |
Output is correct |
9 |
Correct |
50 ms |
103232 KB |
Output is correct |
10 |
Correct |
50 ms |
103240 KB |
Output is correct |
11 |
Correct |
59 ms |
105784 KB |
Output is correct |
12 |
Correct |
58 ms |
105692 KB |
Output is correct |
13 |
Correct |
61 ms |
105796 KB |
Output is correct |
14 |
Correct |
62 ms |
105676 KB |
Output is correct |
15 |
Correct |
58 ms |
105780 KB |
Output is correct |
16 |
Correct |
58 ms |
105768 KB |
Output is correct |
17 |
Correct |
59 ms |
105784 KB |
Output is correct |
18 |
Correct |
58 ms |
105764 KB |
Output is correct |
19 |
Correct |
58 ms |
105816 KB |
Output is correct |
20 |
Correct |
61 ms |
105768 KB |
Output is correct |
21 |
Correct |
59 ms |
105780 KB |
Output is correct |
22 |
Correct |
60 ms |
105872 KB |
Output is correct |
23 |
Correct |
59 ms |
105784 KB |
Output is correct |
24 |
Correct |
59 ms |
105764 KB |
Output is correct |
25 |
Correct |
61 ms |
105704 KB |
Output is correct |
26 |
Correct |
60 ms |
105700 KB |
Output is correct |
27 |
Correct |
59 ms |
105744 KB |
Output is correct |
28 |
Correct |
64 ms |
105804 KB |
Output is correct |
29 |
Correct |
60 ms |
105660 KB |
Output is correct |
30 |
Correct |
60 ms |
105672 KB |
Output is correct |
31 |
Correct |
2655 ms |
371736 KB |
Output is correct |
32 |
Correct |
1408 ms |
375492 KB |
Output is correct |
33 |
Correct |
2680 ms |
375032 KB |
Output is correct |
34 |
Correct |
1415 ms |
375468 KB |
Output is correct |
35 |
Correct |
2501 ms |
372944 KB |
Output is correct |
36 |
Correct |
1393 ms |
375604 KB |
Output is correct |
37 |
Correct |
2485 ms |
371112 KB |
Output is correct |
38 |
Correct |
1343 ms |
374264 KB |
Output is correct |
39 |
Correct |
1958 ms |
364904 KB |
Output is correct |
40 |
Correct |
1937 ms |
362260 KB |
Output is correct |
41 |
Correct |
1944 ms |
364992 KB |
Output is correct |
42 |
Correct |
1981 ms |
368612 KB |
Output is correct |
43 |
Correct |
1973 ms |
362120 KB |
Output is correct |
44 |
Correct |
1958 ms |
368792 KB |
Output is correct |
45 |
Correct |
2083 ms |
368556 KB |
Output is correct |
46 |
Correct |
1978 ms |
362956 KB |
Output is correct |
47 |
Correct |
1981 ms |
363740 KB |
Output is correct |
48 |
Correct |
1954 ms |
363808 KB |
Output is correct |
49 |
Correct |
2075 ms |
366904 KB |
Output is correct |
50 |
Correct |
2000 ms |
369200 KB |
Output is correct |
51 |
Correct |
53 ms |
103236 KB |
Output is correct |
52 |
Correct |
1739 ms |
371116 KB |
Output is correct |
53 |
Correct |
1671 ms |
371688 KB |
Output is correct |
54 |
Correct |
1667 ms |
371312 KB |
Output is correct |
55 |
Correct |
1713 ms |
371532 KB |
Output is correct |
56 |
Correct |
1693 ms |
371416 KB |
Output is correct |
57 |
Correct |
1704 ms |
371352 KB |
Output is correct |
58 |
Correct |
2358 ms |
378624 KB |
Output is correct |
59 |
Correct |
2428 ms |
378036 KB |
Output is correct |
60 |
Correct |
2324 ms |
380732 KB |
Output is correct |
61 |
Correct |
2296 ms |
383476 KB |
Output is correct |
62 |
Correct |
1447 ms |
375500 KB |
Output is correct |
63 |
Correct |
1402 ms |
375632 KB |
Output is correct |
64 |
Correct |
1365 ms |
374220 KB |
Output is correct |
65 |
Correct |
1417 ms |
375284 KB |
Output is correct |
66 |
Correct |
1628 ms |
374332 KB |
Output is correct |
67 |
Correct |
1642 ms |
374328 KB |
Output is correct |
68 |
Correct |
1619 ms |
372896 KB |
Output is correct |
69 |
Correct |
1649 ms |
375472 KB |
Output is correct |
70 |
Correct |
1661 ms |
375120 KB |
Output is correct |
71 |
Correct |
1625 ms |
372772 KB |
Output is correct |
72 |
Correct |
1627 ms |
373036 KB |
Output is correct |
73 |
Correct |
1667 ms |
376972 KB |
Output is correct |
74 |
Correct |
1622 ms |
372904 KB |
Output is correct |
75 |
Correct |
1685 ms |
376292 KB |
Output is correct |
76 |
Correct |
2504 ms |
372904 KB |
Output is correct |
77 |
Correct |
2496 ms |
371508 KB |
Output is correct |
78 |
Correct |
1993 ms |
363892 KB |
Output is correct |
79 |
Correct |
2044 ms |
360808 KB |
Output is correct |
80 |
Correct |
1920 ms |
368680 KB |
Output is correct |
81 |
Correct |
2027 ms |
365196 KB |
Output is correct |
82 |
Correct |
2012 ms |
365472 KB |
Output is correct |
83 |
Correct |
1945 ms |
361572 KB |
Output is correct |
84 |
Correct |
2052 ms |
368652 KB |
Output is correct |
85 |
Correct |
1978 ms |
366504 KB |
Output is correct |
86 |
Correct |
2058 ms |
361140 KB |
Output is correct |
87 |
Correct |
1964 ms |
363068 KB |
Output is correct |
88 |
Correct |
2573 ms |
370420 KB |
Output is correct |
89 |
Correct |
2472 ms |
365848 KB |
Output is correct |
90 |
Correct |
2541 ms |
365700 KB |
Output is correct |
91 |
Correct |
2637 ms |
367868 KB |
Output is correct |
92 |
Correct |
2760 ms |
366912 KB |
Output is correct |
93 |
Correct |
2476 ms |
366764 KB |
Output is correct |
94 |
Correct |
2465 ms |
365916 KB |
Output is correct |
95 |
Correct |
2471 ms |
367608 KB |
Output is correct |
96 |
Correct |
2530 ms |
366684 KB |
Output is correct |
97 |
Correct |
2498 ms |
368168 KB |
Output is correct |