#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 1e9;
vector<int> g[N];
int par[N][18], in[N], out[N], tsz;
void Dfs(int s, int e) {
in[s] = ++tsz;
par[s][0] = e;
for (int i = 1; i < 18; i++) par[s][i] = par[par[s][i - 1]][i - 1];
for (int u : g[s]) {
if (u == e) continue;
Dfs(u, s);
}
out[s] = tsz;
}
bool Ancestor(int u, int v) {
return in[u] <= in[v] && out[v] <= out[u];
}
int Lca(int u, int v) {
if (Ancestor(u, v)) return u;
if (Ancestor(v, u)) return v;
for (int i = 17; i >= 0; i--) {
if (par[u][i] && !Ancestor(par[u][i], v)) u = par[u][i];
}
return par[u][0];
}
int c[N], predak[N];
vector<int> boja[N];
set<int> koji[N];
void Dfs1(int s, int e) {
for (int u : g[s]) {
if (u == e) continue;
if (c[s] != c[u] && Ancestor(predak[c[u]], s)) koji[c[u]].insert(c[s]);
Dfs1(u, s);
}
}
vector<int> a[N], b[N], order;
int visited[N], id[N], szk[N], brk;
void Dfs2(int s) {
if (visited[s] == 1) return;
visited[s] = 1;
for (int u : a[s]) {
Dfs2(u);
}
order.push_back(s);
}
void Dfs3(int s) {
if (id[s] > 0) return;
id[s] = brk;
szk[brk] += 1;
for (int u : b[s]) {
Dfs3(u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
Dfs(1, 0);
for (int i = 1; i <= n; i++) {
cin >> c[i];
boja[c[i]].push_back(i);
}
for (int i = 1; i <= k; i++) {
predak[i] = boja[i][0];
for (int j = 1; j < boja[i].size(); j++) predak[i] = Lca(predak[i], boja[i][j]);
}
Dfs1(1, 0);
for (int i = 1; i <= k; i++) {
for (int j : koji[i]) {
a[i].push_back(j);
b[j].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
if (visited[i] == 0) {
Dfs2(i);
}
}
reverse(order.begin(), order.end());
for (int i : order) {
if (id[i] == 0) {
brk += 1;
Dfs3(i);
}
}
vector<int> cnt(brk + 1);
for (int i = 1; i <= k; i++) {
for (int j : a[i]) {
if (id[i] != id[j]) {
cnt[id[i]] += 1;
}
}
}
int ans = inf;
for (int i = 1; i <= brk; i++) if (cnt[i] == 0) smin(ans, szk[i] - 1);
cout << ans << en;
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:81:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for (int j = 1; j < boja[i].size(); j++) predak[i] = Lca(predak[i], boja[i][j]);
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28448 KB |
Output is correct |
3 |
Correct |
22 ms |
28528 KB |
Output is correct |
4 |
Correct |
14 ms |
28500 KB |
Output is correct |
5 |
Correct |
14 ms |
28520 KB |
Output is correct |
6 |
Correct |
13 ms |
28460 KB |
Output is correct |
7 |
Correct |
13 ms |
28464 KB |
Output is correct |
8 |
Correct |
13 ms |
28512 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
13 ms |
28516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28448 KB |
Output is correct |
3 |
Correct |
22 ms |
28528 KB |
Output is correct |
4 |
Correct |
14 ms |
28500 KB |
Output is correct |
5 |
Correct |
14 ms |
28520 KB |
Output is correct |
6 |
Correct |
13 ms |
28460 KB |
Output is correct |
7 |
Correct |
13 ms |
28464 KB |
Output is correct |
8 |
Correct |
13 ms |
28512 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
13 ms |
28516 KB |
Output is correct |
11 |
Correct |
15 ms |
28912 KB |
Output is correct |
12 |
Correct |
18 ms |
28916 KB |
Output is correct |
13 |
Correct |
16 ms |
28884 KB |
Output is correct |
14 |
Correct |
18 ms |
28880 KB |
Output is correct |
15 |
Correct |
15 ms |
28916 KB |
Output is correct |
16 |
Correct |
16 ms |
28908 KB |
Output is correct |
17 |
Correct |
15 ms |
28952 KB |
Output is correct |
18 |
Correct |
15 ms |
28884 KB |
Output is correct |
19 |
Correct |
15 ms |
28868 KB |
Output is correct |
20 |
Correct |
14 ms |
28920 KB |
Output is correct |
21 |
Correct |
16 ms |
28864 KB |
Output is correct |
22 |
Correct |
15 ms |
29012 KB |
Output is correct |
23 |
Correct |
15 ms |
29048 KB |
Output is correct |
24 |
Correct |
15 ms |
29012 KB |
Output is correct |
25 |
Correct |
16 ms |
28980 KB |
Output is correct |
26 |
Correct |
15 ms |
29044 KB |
Output is correct |
27 |
Correct |
16 ms |
29040 KB |
Output is correct |
28 |
Correct |
15 ms |
28932 KB |
Output is correct |
29 |
Correct |
15 ms |
28884 KB |
Output is correct |
30 |
Correct |
15 ms |
28916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
274 ms |
86596 KB |
Output is correct |
2 |
Correct |
132 ms |
87116 KB |
Output is correct |
3 |
Correct |
326 ms |
86220 KB |
Output is correct |
4 |
Correct |
144 ms |
87084 KB |
Output is correct |
5 |
Correct |
337 ms |
82284 KB |
Output is correct |
6 |
Correct |
137 ms |
86884 KB |
Output is correct |
7 |
Correct |
272 ms |
81700 KB |
Output is correct |
8 |
Correct |
128 ms |
84356 KB |
Output is correct |
9 |
Correct |
274 ms |
77952 KB |
Output is correct |
10 |
Correct |
277 ms |
75536 KB |
Output is correct |
11 |
Correct |
288 ms |
78468 KB |
Output is correct |
12 |
Correct |
300 ms |
81100 KB |
Output is correct |
13 |
Correct |
273 ms |
75220 KB |
Output is correct |
14 |
Correct |
274 ms |
81484 KB |
Output is correct |
15 |
Correct |
288 ms |
81092 KB |
Output is correct |
16 |
Correct |
277 ms |
76192 KB |
Output is correct |
17 |
Correct |
274 ms |
76692 KB |
Output is correct |
18 |
Correct |
289 ms |
76828 KB |
Output is correct |
19 |
Correct |
284 ms |
80168 KB |
Output is correct |
20 |
Correct |
280 ms |
82308 KB |
Output is correct |
21 |
Correct |
13 ms |
28516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
28500 KB |
Output is correct |
2 |
Correct |
13 ms |
28448 KB |
Output is correct |
3 |
Correct |
22 ms |
28528 KB |
Output is correct |
4 |
Correct |
14 ms |
28500 KB |
Output is correct |
5 |
Correct |
14 ms |
28520 KB |
Output is correct |
6 |
Correct |
13 ms |
28460 KB |
Output is correct |
7 |
Correct |
13 ms |
28464 KB |
Output is correct |
8 |
Correct |
13 ms |
28512 KB |
Output is correct |
9 |
Correct |
14 ms |
28500 KB |
Output is correct |
10 |
Correct |
13 ms |
28516 KB |
Output is correct |
11 |
Correct |
15 ms |
28912 KB |
Output is correct |
12 |
Correct |
18 ms |
28916 KB |
Output is correct |
13 |
Correct |
16 ms |
28884 KB |
Output is correct |
14 |
Correct |
18 ms |
28880 KB |
Output is correct |
15 |
Correct |
15 ms |
28916 KB |
Output is correct |
16 |
Correct |
16 ms |
28908 KB |
Output is correct |
17 |
Correct |
15 ms |
28952 KB |
Output is correct |
18 |
Correct |
15 ms |
28884 KB |
Output is correct |
19 |
Correct |
15 ms |
28868 KB |
Output is correct |
20 |
Correct |
14 ms |
28920 KB |
Output is correct |
21 |
Correct |
16 ms |
28864 KB |
Output is correct |
22 |
Correct |
15 ms |
29012 KB |
Output is correct |
23 |
Correct |
15 ms |
29048 KB |
Output is correct |
24 |
Correct |
15 ms |
29012 KB |
Output is correct |
25 |
Correct |
16 ms |
28980 KB |
Output is correct |
26 |
Correct |
15 ms |
29044 KB |
Output is correct |
27 |
Correct |
16 ms |
29040 KB |
Output is correct |
28 |
Correct |
15 ms |
28932 KB |
Output is correct |
29 |
Correct |
15 ms |
28884 KB |
Output is correct |
30 |
Correct |
15 ms |
28916 KB |
Output is correct |
31 |
Correct |
274 ms |
86596 KB |
Output is correct |
32 |
Correct |
132 ms |
87116 KB |
Output is correct |
33 |
Correct |
326 ms |
86220 KB |
Output is correct |
34 |
Correct |
144 ms |
87084 KB |
Output is correct |
35 |
Correct |
337 ms |
82284 KB |
Output is correct |
36 |
Correct |
137 ms |
86884 KB |
Output is correct |
37 |
Correct |
272 ms |
81700 KB |
Output is correct |
38 |
Correct |
128 ms |
84356 KB |
Output is correct |
39 |
Correct |
274 ms |
77952 KB |
Output is correct |
40 |
Correct |
277 ms |
75536 KB |
Output is correct |
41 |
Correct |
288 ms |
78468 KB |
Output is correct |
42 |
Correct |
300 ms |
81100 KB |
Output is correct |
43 |
Correct |
273 ms |
75220 KB |
Output is correct |
44 |
Correct |
274 ms |
81484 KB |
Output is correct |
45 |
Correct |
288 ms |
81092 KB |
Output is correct |
46 |
Correct |
277 ms |
76192 KB |
Output is correct |
47 |
Correct |
274 ms |
76692 KB |
Output is correct |
48 |
Correct |
289 ms |
76828 KB |
Output is correct |
49 |
Correct |
284 ms |
80168 KB |
Output is correct |
50 |
Correct |
280 ms |
82308 KB |
Output is correct |
51 |
Correct |
13 ms |
28516 KB |
Output is correct |
52 |
Correct |
319 ms |
69672 KB |
Output is correct |
53 |
Correct |
333 ms |
69792 KB |
Output is correct |
54 |
Correct |
368 ms |
69700 KB |
Output is correct |
55 |
Correct |
309 ms |
69628 KB |
Output is correct |
56 |
Correct |
316 ms |
69684 KB |
Output is correct |
57 |
Correct |
332 ms |
69640 KB |
Output is correct |
58 |
Correct |
244 ms |
70192 KB |
Output is correct |
59 |
Correct |
255 ms |
70980 KB |
Output is correct |
60 |
Correct |
322 ms |
72932 KB |
Output is correct |
61 |
Correct |
333 ms |
72740 KB |
Output is correct |
62 |
Correct |
127 ms |
87052 KB |
Output is correct |
63 |
Correct |
125 ms |
87052 KB |
Output is correct |
64 |
Correct |
131 ms |
84920 KB |
Output is correct |
65 |
Correct |
124 ms |
86752 KB |
Output is correct |
66 |
Correct |
203 ms |
70404 KB |
Output is correct |
67 |
Correct |
214 ms |
70320 KB |
Output is correct |
68 |
Correct |
227 ms |
70460 KB |
Output is correct |
69 |
Correct |
188 ms |
70360 KB |
Output is correct |
70 |
Correct |
211 ms |
70376 KB |
Output is correct |
71 |
Correct |
200 ms |
70476 KB |
Output is correct |
72 |
Correct |
201 ms |
70284 KB |
Output is correct |
73 |
Correct |
198 ms |
69440 KB |
Output is correct |
74 |
Correct |
206 ms |
70472 KB |
Output is correct |
75 |
Correct |
202 ms |
70340 KB |
Output is correct |
76 |
Correct |
258 ms |
77084 KB |
Output is correct |
77 |
Correct |
278 ms |
75148 KB |
Output is correct |
78 |
Correct |
281 ms |
76672 KB |
Output is correct |
79 |
Correct |
288 ms |
74744 KB |
Output is correct |
80 |
Correct |
289 ms |
81644 KB |
Output is correct |
81 |
Correct |
265 ms |
78172 KB |
Output is correct |
82 |
Correct |
282 ms |
78432 KB |
Output is correct |
83 |
Correct |
280 ms |
74992 KB |
Output is correct |
84 |
Correct |
288 ms |
80968 KB |
Output is correct |
85 |
Correct |
270 ms |
79340 KB |
Output is correct |
86 |
Correct |
259 ms |
74568 KB |
Output is correct |
87 |
Correct |
281 ms |
76260 KB |
Output is correct |
88 |
Correct |
281 ms |
76388 KB |
Output is correct |
89 |
Correct |
275 ms |
72192 KB |
Output is correct |
90 |
Correct |
265 ms |
71752 KB |
Output is correct |
91 |
Correct |
286 ms |
74448 KB |
Output is correct |
92 |
Correct |
283 ms |
73160 KB |
Output is correct |
93 |
Correct |
285 ms |
72884 KB |
Output is correct |
94 |
Correct |
258 ms |
72120 KB |
Output is correct |
95 |
Correct |
275 ms |
73548 KB |
Output is correct |
96 |
Correct |
281 ms |
72520 KB |
Output is correct |
97 |
Correct |
272 ms |
74312 KB |
Output is correct |