#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int MXN = 2e5 + 7;
int n, k;
vector<int> ed[MXN];
int c[MXN], tot[MXN], sz[MXN], ans = MXN;
bool del[MXN], used[MXN];
vector<int> nodes;
bool usedc[MXN];
vector<int> cc[MXN];
int pp[MXN];
int get_sz(int v, int p = -1) {
sz[v] = 1;
for (int u : ed[v]) {
if (u != p && !del[u]) sz[v] += get_sz(u, v);
}
return sz[v];
}
int cent(int v, int nn, int p = -1) {
for (int u : ed[v]) {
if (u != p && !del[u] && 2 * sz[u] > nn) return cent(u, nn, v);
}
return v;
}
void dfs(int v, int p = -1) {
pp[v] = p;
nodes.pb(v);
used[v] = usedc[c[v]] = 0;
cc[c[v]].pb(v);
for (int u : ed[v]) {
if (u != p && !del[u]) dfs(u, v);
}
}
void solve(int v) {
get_sz(v);
nodes.clear();
dfs(v);
queue<int> q;
q.push(c[v]);
used[v] = usedc[c[v]] = 1;
int need = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int u : cc[x]) {
int vv = u;
while (!used[vv]) {
used[vv] = 1;
if (!usedc[c[vv]]) {
need++;
usedc[c[vv]] = 1;
q.push(c[vv]);
}
vv = pp[vv];
}
}
}
bool ok = 1;
for (int u : nodes) {
if (used[u]) ok &= cc[c[u]].size() == tot[c[u]];
}
for (int u : nodes) cc[c[u]].clear();
if (ok) ans = min(ans, need);
del[v] = 1;
for (int u : ed[v]) {
if (!del[u]) solve(cent(u, sz[u]));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
ed[a].pb(b);
ed[b].pb(a);
}
for (int i = 0; i < n; i++) {
cin >> c[i];
c[i]++;
tot[c[i]]++;
}
solve(cent(0, get_sz(0)));
cout << ans << endl;
return 0;
}
Compilation message
capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:68:44: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if (used[u]) ok &= cc[c[u]].size() == tot[c[u]];
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
9 ms |
9964 KB |
Output is correct |
12 |
Correct |
9 ms |
9964 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
9 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
9964 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
9 ms |
10112 KB |
Output is correct |
18 |
Correct |
10 ms |
9964 KB |
Output is correct |
19 |
Correct |
9 ms |
9964 KB |
Output is correct |
20 |
Correct |
9 ms |
9964 KB |
Output is correct |
21 |
Correct |
8 ms |
9964 KB |
Output is correct |
22 |
Correct |
9 ms |
10092 KB |
Output is correct |
23 |
Correct |
11 ms |
9964 KB |
Output is correct |
24 |
Correct |
9 ms |
9964 KB |
Output is correct |
25 |
Correct |
10 ms |
9964 KB |
Output is correct |
26 |
Correct |
10 ms |
9964 KB |
Output is correct |
27 |
Correct |
11 ms |
9964 KB |
Output is correct |
28 |
Correct |
11 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
9964 KB |
Output is correct |
30 |
Correct |
9 ms |
9964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
976 ms |
39012 KB |
Output is correct |
2 |
Correct |
279 ms |
39392 KB |
Output is correct |
3 |
Correct |
918 ms |
38756 KB |
Output is correct |
4 |
Correct |
283 ms |
39392 KB |
Output is correct |
5 |
Correct |
896 ms |
36324 KB |
Output is correct |
6 |
Correct |
269 ms |
39264 KB |
Output is correct |
7 |
Correct |
907 ms |
36580 KB |
Output is correct |
8 |
Correct |
277 ms |
38880 KB |
Output is correct |
9 |
Correct |
985 ms |
34912 KB |
Output is correct |
10 |
Correct |
952 ms |
32736 KB |
Output is correct |
11 |
Correct |
989 ms |
35296 KB |
Output is correct |
12 |
Correct |
994 ms |
37176 KB |
Output is correct |
13 |
Correct |
1016 ms |
32480 KB |
Output is correct |
14 |
Correct |
956 ms |
37600 KB |
Output is correct |
15 |
Correct |
999 ms |
37256 KB |
Output is correct |
16 |
Correct |
944 ms |
33376 KB |
Output is correct |
17 |
Correct |
986 ms |
33632 KB |
Output is correct |
18 |
Correct |
984 ms |
33888 KB |
Output is correct |
19 |
Correct |
986 ms |
36448 KB |
Output is correct |
20 |
Correct |
958 ms |
38240 KB |
Output is correct |
21 |
Correct |
8 ms |
9708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9708 KB |
Output is correct |
2 |
Correct |
7 ms |
9708 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
7 ms |
9708 KB |
Output is correct |
5 |
Correct |
7 ms |
9708 KB |
Output is correct |
6 |
Correct |
7 ms |
9708 KB |
Output is correct |
7 |
Correct |
7 ms |
9708 KB |
Output is correct |
8 |
Correct |
7 ms |
9708 KB |
Output is correct |
9 |
Correct |
7 ms |
9708 KB |
Output is correct |
10 |
Correct |
7 ms |
9708 KB |
Output is correct |
11 |
Correct |
9 ms |
9964 KB |
Output is correct |
12 |
Correct |
9 ms |
9964 KB |
Output is correct |
13 |
Correct |
9 ms |
9964 KB |
Output is correct |
14 |
Correct |
9 ms |
9964 KB |
Output is correct |
15 |
Correct |
9 ms |
9964 KB |
Output is correct |
16 |
Correct |
10 ms |
9984 KB |
Output is correct |
17 |
Correct |
9 ms |
10112 KB |
Output is correct |
18 |
Correct |
10 ms |
9964 KB |
Output is correct |
19 |
Correct |
9 ms |
9964 KB |
Output is correct |
20 |
Correct |
9 ms |
9964 KB |
Output is correct |
21 |
Correct |
8 ms |
9964 KB |
Output is correct |
22 |
Correct |
9 ms |
10092 KB |
Output is correct |
23 |
Correct |
11 ms |
9964 KB |
Output is correct |
24 |
Correct |
9 ms |
9964 KB |
Output is correct |
25 |
Correct |
10 ms |
9964 KB |
Output is correct |
26 |
Correct |
10 ms |
9964 KB |
Output is correct |
27 |
Correct |
11 ms |
9964 KB |
Output is correct |
28 |
Correct |
11 ms |
9964 KB |
Output is correct |
29 |
Correct |
9 ms |
9964 KB |
Output is correct |
30 |
Correct |
9 ms |
9964 KB |
Output is correct |
31 |
Correct |
976 ms |
39012 KB |
Output is correct |
32 |
Correct |
279 ms |
39392 KB |
Output is correct |
33 |
Correct |
918 ms |
38756 KB |
Output is correct |
34 |
Correct |
283 ms |
39392 KB |
Output is correct |
35 |
Correct |
896 ms |
36324 KB |
Output is correct |
36 |
Correct |
269 ms |
39264 KB |
Output is correct |
37 |
Correct |
907 ms |
36580 KB |
Output is correct |
38 |
Correct |
277 ms |
38880 KB |
Output is correct |
39 |
Correct |
985 ms |
34912 KB |
Output is correct |
40 |
Correct |
952 ms |
32736 KB |
Output is correct |
41 |
Correct |
989 ms |
35296 KB |
Output is correct |
42 |
Correct |
994 ms |
37176 KB |
Output is correct |
43 |
Correct |
1016 ms |
32480 KB |
Output is correct |
44 |
Correct |
956 ms |
37600 KB |
Output is correct |
45 |
Correct |
999 ms |
37256 KB |
Output is correct |
46 |
Correct |
944 ms |
33376 KB |
Output is correct |
47 |
Correct |
986 ms |
33632 KB |
Output is correct |
48 |
Correct |
984 ms |
33888 KB |
Output is correct |
49 |
Correct |
986 ms |
36448 KB |
Output is correct |
50 |
Correct |
958 ms |
38240 KB |
Output is correct |
51 |
Correct |
8 ms |
9708 KB |
Output is correct |
52 |
Correct |
649 ms |
24984 KB |
Output is correct |
53 |
Correct |
744 ms |
25056 KB |
Output is correct |
54 |
Correct |
710 ms |
25056 KB |
Output is correct |
55 |
Correct |
737 ms |
25056 KB |
Output is correct |
56 |
Correct |
750 ms |
24928 KB |
Output is correct |
57 |
Correct |
743 ms |
25060 KB |
Output is correct |
58 |
Correct |
698 ms |
28256 KB |
Output is correct |
59 |
Correct |
696 ms |
28576 KB |
Output is correct |
60 |
Correct |
893 ms |
28268 KB |
Output is correct |
61 |
Correct |
948 ms |
27872 KB |
Output is correct |
62 |
Correct |
266 ms |
39380 KB |
Output is correct |
63 |
Correct |
268 ms |
39392 KB |
Output is correct |
64 |
Correct |
281 ms |
39136 KB |
Output is correct |
65 |
Correct |
289 ms |
39520 KB |
Output is correct |
66 |
Correct |
500 ms |
32608 KB |
Output is correct |
67 |
Correct |
520 ms |
32352 KB |
Output is correct |
68 |
Correct |
511 ms |
32608 KB |
Output is correct |
69 |
Correct |
474 ms |
32668 KB |
Output is correct |
70 |
Correct |
497 ms |
32352 KB |
Output is correct |
71 |
Correct |
466 ms |
32644 KB |
Output is correct |
72 |
Correct |
491 ms |
32608 KB |
Output is correct |
73 |
Correct |
465 ms |
31712 KB |
Output is correct |
74 |
Correct |
516 ms |
32556 KB |
Output is correct |
75 |
Correct |
502 ms |
32480 KB |
Output is correct |
76 |
Correct |
791 ms |
31544 KB |
Output is correct |
77 |
Correct |
812 ms |
30108 KB |
Output is correct |
78 |
Correct |
955 ms |
33760 KB |
Output is correct |
79 |
Correct |
997 ms |
32136 KB |
Output is correct |
80 |
Correct |
973 ms |
37820 KB |
Output is correct |
81 |
Correct |
983 ms |
34912 KB |
Output is correct |
82 |
Correct |
1063 ms |
35004 KB |
Output is correct |
83 |
Correct |
947 ms |
32368 KB |
Output is correct |
84 |
Correct |
1006 ms |
37148 KB |
Output is correct |
85 |
Correct |
1041 ms |
35848 KB |
Output is correct |
86 |
Correct |
980 ms |
32032 KB |
Output is correct |
87 |
Correct |
994 ms |
33504 KB |
Output is correct |
88 |
Correct |
835 ms |
34372 KB |
Output is correct |
89 |
Correct |
818 ms |
31072 KB |
Output is correct |
90 |
Correct |
829 ms |
30972 KB |
Output is correct |
91 |
Correct |
829 ms |
32876 KB |
Output is correct |
92 |
Correct |
825 ms |
31712 KB |
Output is correct |
93 |
Correct |
884 ms |
31584 KB |
Output is correct |
94 |
Correct |
908 ms |
31036 KB |
Output is correct |
95 |
Correct |
856 ms |
32492 KB |
Output is correct |
96 |
Correct |
893 ms |
31320 KB |
Output is correct |
97 |
Correct |
871 ms |
32848 KB |
Output is correct |