#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()
const int N = 2e5 + 7;
int n, k;
vector<int> adj[N], col[N];
int c[N], cnt[N], pt[N];
int sz[N];
bool vis[N], rem[N];
int ans;
int dfs_sz(int v, int p = 0) {
sz[v] = 1;
for (int u : adj[v]) {
if (u != p && !rem[u])
sz[v] += dfs_sz(u, v);
}
return sz[v];
}
int get_centroid(int v, int tot, int p = 0) {
for (int u : adj[v]) {
if (u != p && !rem[u]) {
if (sz[u] * 2 > tot)
return get_centroid(u, tot, v);
}
}
return v;
}
void prep(int v, int p = 0) {
pt[v] = p;
cnt[c[v]]++;
for (int u : adj[v]) {
if (u != p && !rem[u])
prep(u, v);
}
}
void cls(int v, int p = 0) {
cnt[c[v]] = 0;
vis[c[v]] = 0;
for (int u : adj[v]) {
if (u != p && !rem[u])
cls(u, v);
}
}
void solve(int a = 1) {
int s = get_centroid(a, dfs_sz(a));
prep(s);
queue<int> q;
q.push(c[s]);
vis[c[s]] = 1;
bool ok = 1;
int used = 0;
while (q.size()) {
int x = q.front();
q.pop();
if (cnt[x] != col[x].size()) {
ok = 0;
break;
}
used++;
for (int v : col[x]) {
v = pt[v];
if (v > 0 && !vis[c[v]]) {
vis[c[v]] = 1;
q.push(c[v]);
}
}
}
if (ok)
ans = min(ans, used - 1);
cls(s);
rem[s] = 1;
for (int u : adj[s]) {
if (!rem[u])
solve(u);
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
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++) {
cin >> c[i];
col[c[i]].push_back(i);
}
ans = k - 1;
solve();
cout << ans << '\n';
return 0;
}
Compilation message
capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:69:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | if (cnt[x] != col[x].size()) {
| ~~~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
3 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13148 KB |
Output is correct |
9 |
Correct |
3 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
12992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
3 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13148 KB |
Output is correct |
9 |
Correct |
3 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
12992 KB |
Output is correct |
11 |
Correct |
3 ms |
13148 KB |
Output is correct |
12 |
Correct |
4 ms |
13148 KB |
Output is correct |
13 |
Correct |
6 ms |
13148 KB |
Output is correct |
14 |
Correct |
4 ms |
13148 KB |
Output is correct |
15 |
Correct |
4 ms |
13148 KB |
Output is correct |
16 |
Correct |
4 ms |
13148 KB |
Output is correct |
17 |
Correct |
3 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
3 ms |
13148 KB |
Output is correct |
20 |
Correct |
3 ms |
13144 KB |
Output is correct |
21 |
Correct |
5 ms |
13212 KB |
Output is correct |
22 |
Correct |
4 ms |
13464 KB |
Output is correct |
23 |
Correct |
3 ms |
13148 KB |
Output is correct |
24 |
Correct |
4 ms |
13148 KB |
Output is correct |
25 |
Correct |
4 ms |
13404 KB |
Output is correct |
26 |
Correct |
4 ms |
13424 KB |
Output is correct |
27 |
Correct |
4 ms |
13400 KB |
Output is correct |
28 |
Correct |
4 ms |
13204 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
4 ms |
13144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
38484 KB |
Output is correct |
2 |
Correct |
151 ms |
38764 KB |
Output is correct |
3 |
Correct |
276 ms |
38272 KB |
Output is correct |
4 |
Correct |
147 ms |
38932 KB |
Output is correct |
5 |
Correct |
345 ms |
35680 KB |
Output is correct |
6 |
Correct |
149 ms |
38912 KB |
Output is correct |
7 |
Correct |
298 ms |
36064 KB |
Output is correct |
8 |
Correct |
144 ms |
38352 KB |
Output is correct |
9 |
Correct |
385 ms |
34552 KB |
Output is correct |
10 |
Correct |
404 ms |
32340 KB |
Output is correct |
11 |
Correct |
415 ms |
35000 KB |
Output is correct |
12 |
Correct |
371 ms |
36944 KB |
Output is correct |
13 |
Correct |
485 ms |
32232 KB |
Output is correct |
14 |
Correct |
354 ms |
37268 KB |
Output is correct |
15 |
Correct |
440 ms |
37204 KB |
Output is correct |
16 |
Correct |
443 ms |
33052 KB |
Output is correct |
17 |
Correct |
372 ms |
33300 KB |
Output is correct |
18 |
Correct |
429 ms |
33664 KB |
Output is correct |
19 |
Correct |
379 ms |
35920 KB |
Output is correct |
20 |
Correct |
385 ms |
37760 KB |
Output is correct |
21 |
Correct |
3 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
13148 KB |
Output is correct |
2 |
Correct |
2 ms |
13148 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
3 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
13148 KB |
Output is correct |
6 |
Correct |
3 ms |
13148 KB |
Output is correct |
7 |
Correct |
2 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13148 KB |
Output is correct |
9 |
Correct |
3 ms |
13404 KB |
Output is correct |
10 |
Correct |
2 ms |
12992 KB |
Output is correct |
11 |
Correct |
3 ms |
13148 KB |
Output is correct |
12 |
Correct |
4 ms |
13148 KB |
Output is correct |
13 |
Correct |
6 ms |
13148 KB |
Output is correct |
14 |
Correct |
4 ms |
13148 KB |
Output is correct |
15 |
Correct |
4 ms |
13148 KB |
Output is correct |
16 |
Correct |
4 ms |
13148 KB |
Output is correct |
17 |
Correct |
3 ms |
13148 KB |
Output is correct |
18 |
Correct |
3 ms |
13148 KB |
Output is correct |
19 |
Correct |
3 ms |
13148 KB |
Output is correct |
20 |
Correct |
3 ms |
13144 KB |
Output is correct |
21 |
Correct |
5 ms |
13212 KB |
Output is correct |
22 |
Correct |
4 ms |
13464 KB |
Output is correct |
23 |
Correct |
3 ms |
13148 KB |
Output is correct |
24 |
Correct |
4 ms |
13148 KB |
Output is correct |
25 |
Correct |
4 ms |
13404 KB |
Output is correct |
26 |
Correct |
4 ms |
13424 KB |
Output is correct |
27 |
Correct |
4 ms |
13400 KB |
Output is correct |
28 |
Correct |
4 ms |
13204 KB |
Output is correct |
29 |
Correct |
5 ms |
13148 KB |
Output is correct |
30 |
Correct |
4 ms |
13144 KB |
Output is correct |
31 |
Correct |
312 ms |
38484 KB |
Output is correct |
32 |
Correct |
151 ms |
38764 KB |
Output is correct |
33 |
Correct |
276 ms |
38272 KB |
Output is correct |
34 |
Correct |
147 ms |
38932 KB |
Output is correct |
35 |
Correct |
345 ms |
35680 KB |
Output is correct |
36 |
Correct |
149 ms |
38912 KB |
Output is correct |
37 |
Correct |
298 ms |
36064 KB |
Output is correct |
38 |
Correct |
144 ms |
38352 KB |
Output is correct |
39 |
Correct |
385 ms |
34552 KB |
Output is correct |
40 |
Correct |
404 ms |
32340 KB |
Output is correct |
41 |
Correct |
415 ms |
35000 KB |
Output is correct |
42 |
Correct |
371 ms |
36944 KB |
Output is correct |
43 |
Correct |
485 ms |
32232 KB |
Output is correct |
44 |
Correct |
354 ms |
37268 KB |
Output is correct |
45 |
Correct |
440 ms |
37204 KB |
Output is correct |
46 |
Correct |
443 ms |
33052 KB |
Output is correct |
47 |
Correct |
372 ms |
33300 KB |
Output is correct |
48 |
Correct |
429 ms |
33664 KB |
Output is correct |
49 |
Correct |
379 ms |
35920 KB |
Output is correct |
50 |
Correct |
385 ms |
37760 KB |
Output is correct |
51 |
Correct |
3 ms |
13148 KB |
Output is correct |
52 |
Correct |
247 ms |
24916 KB |
Output is correct |
53 |
Correct |
245 ms |
24912 KB |
Output is correct |
54 |
Correct |
242 ms |
24656 KB |
Output is correct |
55 |
Correct |
245 ms |
24912 KB |
Output is correct |
56 |
Correct |
256 ms |
24912 KB |
Output is correct |
57 |
Correct |
247 ms |
24912 KB |
Output is correct |
58 |
Correct |
210 ms |
27972 KB |
Output is correct |
59 |
Correct |
225 ms |
28256 KB |
Output is correct |
60 |
Correct |
272 ms |
27712 KB |
Output is correct |
61 |
Correct |
270 ms |
27724 KB |
Output is correct |
62 |
Correct |
147 ms |
38740 KB |
Output is correct |
63 |
Correct |
152 ms |
38740 KB |
Output is correct |
64 |
Correct |
148 ms |
38672 KB |
Output is correct |
65 |
Correct |
172 ms |
38740 KB |
Output is correct |
66 |
Correct |
191 ms |
31948 KB |
Output is correct |
67 |
Correct |
194 ms |
31756 KB |
Output is correct |
68 |
Correct |
229 ms |
32016 KB |
Output is correct |
69 |
Correct |
215 ms |
32036 KB |
Output is correct |
70 |
Correct |
216 ms |
31944 KB |
Output is correct |
71 |
Correct |
199 ms |
31696 KB |
Output is correct |
72 |
Correct |
225 ms |
31724 KB |
Output is correct |
73 |
Correct |
190 ms |
31436 KB |
Output is correct |
74 |
Correct |
175 ms |
31868 KB |
Output is correct |
75 |
Correct |
171 ms |
31984 KB |
Output is correct |
76 |
Correct |
303 ms |
31184 KB |
Output is correct |
77 |
Correct |
295 ms |
29464 KB |
Output is correct |
78 |
Correct |
335 ms |
33364 KB |
Output is correct |
79 |
Correct |
338 ms |
31572 KB |
Output is correct |
80 |
Correct |
452 ms |
37844 KB |
Output is correct |
81 |
Correct |
402 ms |
34672 KB |
Output is correct |
82 |
Correct |
356 ms |
34980 KB |
Output is correct |
83 |
Correct |
387 ms |
32080 KB |
Output is correct |
84 |
Correct |
396 ms |
37040 KB |
Output is correct |
85 |
Correct |
397 ms |
35396 KB |
Output is correct |
86 |
Correct |
435 ms |
31576 KB |
Output is correct |
87 |
Correct |
374 ms |
33104 KB |
Output is correct |
88 |
Correct |
325 ms |
34176 KB |
Output is correct |
89 |
Correct |
291 ms |
30668 KB |
Output is correct |
90 |
Correct |
302 ms |
30936 KB |
Output is correct |
91 |
Correct |
289 ms |
32680 KB |
Output is correct |
92 |
Correct |
304 ms |
31572 KB |
Output is correct |
93 |
Correct |
295 ms |
31332 KB |
Output is correct |
94 |
Correct |
291 ms |
30548 KB |
Output is correct |
95 |
Correct |
311 ms |
32004 KB |
Output is correct |
96 |
Correct |
304 ms |
30912 KB |
Output is correct |
97 |
Correct |
308 ms |
32968 KB |
Output is correct |