#include <bits/stdc++.h>
using namespace std;
#define eack emplace_back
int N, K, C[200001], ans, A[200001], B[200001];
vector<int> edge[200001];
bool vis[200001];
int sz[200001];
int szf(int x, int p) {
sz[x] = 1;
for (auto i : edge[x]) if (i != p && !vis[i]) sz[x] += szf(i, x);
return sz[x];
}
int get_cen(int x, int p, int t) {
for (auto i : edge[x]) if (i != p && !vis[i] && sz[i] >= t) return get_cen(i, x, t);
return x;
}
bool ban[200001], chk[200001], vis2[200001];
vector<int> clr, vec[200001];
int par[200001];
void dfs(int x, int p) {
par[x] = p;
clr.eack(C[x]);
vec[C[x]].eack(x);
++B[C[x]];
chk[C[x]] = vis2[x] = 0;
for (auto i : edge[x]) if (i != p && !vis[i]) dfs(i, x);
}
void cd(int x) {
x = get_cen(x, x, szf(x, x) + 1 >> 1);
vis[x] = 1;
for (auto i : clr) {
vec[i].clear();
B[i] = ban[i] = 0;
}
clr.clear();
dfs(x, x);
for (auto i : clr) if (A[i] > B[i]) ban[i] = 1;
queue<int> q;
q.push(C[x]);
chk[C[x]] = vis2[x] = 1;
bool flag = 1;
int cnt = 0;
while (q.size()) {
int x = q.front(); q.pop();
if (ban[x]) {
flag = 0;
break;
}
for (auto i : vec[x]) {
for (int j = i; !vis2[j]; j = par[j]) {
vis2[j] = 1;
if (!chk[C[j]]) {
q.push(C[j]);
chk[C[j]] = 1;
++cnt;
}
}
}
}
if (flag) ans = min(ans, cnt);
for (auto i : edge[x]) if (!vis[i]) cd(i);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> K;
ans = K - 1;
for (int i = 1; i < N; ++i) {
int x, y; cin >> x >> y;
edge[x].eack(y);
edge[y].eack(x);
}
for (int i = 1; i <= N; ++i) {
cin >> C[i];
++A[C[i]];
}
cd(1);
cout << ans;
return 0;
}
Compilation message
capital_city.cpp: In function 'void cd(int)':
capital_city.cpp:33:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
x = get_cen(x, x, szf(x, x) + 1 >> 1);
~~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
11 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
11 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
12 ms |
9856 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
13 ms |
9984 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
12 ms |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9984 KB |
Output is correct |
17 |
Correct |
11 ms |
9984 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
11 ms |
9984 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
12 ms |
9984 KB |
Output is correct |
23 |
Correct |
12 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
12 ms |
9984 KB |
Output is correct |
30 |
Correct |
12 ms |
9984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
891 ms |
32824 KB |
Output is correct |
2 |
Correct |
282 ms |
33000 KB |
Output is correct |
3 |
Correct |
790 ms |
32616 KB |
Output is correct |
4 |
Correct |
293 ms |
33132 KB |
Output is correct |
5 |
Correct |
768 ms |
30700 KB |
Output is correct |
6 |
Correct |
279 ms |
33004 KB |
Output is correct |
7 |
Correct |
790 ms |
30780 KB |
Output is correct |
8 |
Correct |
274 ms |
32488 KB |
Output is correct |
9 |
Correct |
819 ms |
29204 KB |
Output is correct |
10 |
Correct |
785 ms |
27624 KB |
Output is correct |
11 |
Correct |
789 ms |
29420 KB |
Output is correct |
12 |
Correct |
805 ms |
31000 KB |
Output is correct |
13 |
Correct |
803 ms |
27368 KB |
Output is correct |
14 |
Correct |
808 ms |
31080 KB |
Output is correct |
15 |
Correct |
825 ms |
31080 KB |
Output is correct |
16 |
Correct |
773 ms |
27888 KB |
Output is correct |
17 |
Correct |
771 ms |
28396 KB |
Output is correct |
18 |
Correct |
786 ms |
28396 KB |
Output is correct |
19 |
Correct |
802 ms |
30444 KB |
Output is correct |
20 |
Correct |
789 ms |
31592 KB |
Output is correct |
21 |
Correct |
10 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
11 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9728 KB |
Output is correct |
4 |
Correct |
10 ms |
9728 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
11 ms |
9856 KB |
Output is correct |
7 |
Correct |
10 ms |
9728 KB |
Output is correct |
8 |
Correct |
10 ms |
9856 KB |
Output is correct |
9 |
Correct |
10 ms |
9856 KB |
Output is correct |
10 |
Correct |
10 ms |
9728 KB |
Output is correct |
11 |
Correct |
12 ms |
9856 KB |
Output is correct |
12 |
Correct |
12 ms |
9856 KB |
Output is correct |
13 |
Correct |
13 ms |
9984 KB |
Output is correct |
14 |
Correct |
12 ms |
9856 KB |
Output is correct |
15 |
Correct |
12 ms |
9984 KB |
Output is correct |
16 |
Correct |
12 ms |
9984 KB |
Output is correct |
17 |
Correct |
11 ms |
9984 KB |
Output is correct |
18 |
Correct |
11 ms |
9984 KB |
Output is correct |
19 |
Correct |
12 ms |
9984 KB |
Output is correct |
20 |
Correct |
11 ms |
9984 KB |
Output is correct |
21 |
Correct |
11 ms |
9984 KB |
Output is correct |
22 |
Correct |
12 ms |
9984 KB |
Output is correct |
23 |
Correct |
12 ms |
9984 KB |
Output is correct |
24 |
Correct |
12 ms |
9984 KB |
Output is correct |
25 |
Correct |
13 ms |
9984 KB |
Output is correct |
26 |
Correct |
13 ms |
9984 KB |
Output is correct |
27 |
Correct |
13 ms |
9984 KB |
Output is correct |
28 |
Correct |
12 ms |
9984 KB |
Output is correct |
29 |
Correct |
12 ms |
9984 KB |
Output is correct |
30 |
Correct |
12 ms |
9984 KB |
Output is correct |
31 |
Correct |
891 ms |
32824 KB |
Output is correct |
32 |
Correct |
282 ms |
33000 KB |
Output is correct |
33 |
Correct |
790 ms |
32616 KB |
Output is correct |
34 |
Correct |
293 ms |
33132 KB |
Output is correct |
35 |
Correct |
768 ms |
30700 KB |
Output is correct |
36 |
Correct |
279 ms |
33004 KB |
Output is correct |
37 |
Correct |
790 ms |
30780 KB |
Output is correct |
38 |
Correct |
274 ms |
32488 KB |
Output is correct |
39 |
Correct |
819 ms |
29204 KB |
Output is correct |
40 |
Correct |
785 ms |
27624 KB |
Output is correct |
41 |
Correct |
789 ms |
29420 KB |
Output is correct |
42 |
Correct |
805 ms |
31000 KB |
Output is correct |
43 |
Correct |
803 ms |
27368 KB |
Output is correct |
44 |
Correct |
808 ms |
31080 KB |
Output is correct |
45 |
Correct |
825 ms |
31080 KB |
Output is correct |
46 |
Correct |
773 ms |
27888 KB |
Output is correct |
47 |
Correct |
771 ms |
28396 KB |
Output is correct |
48 |
Correct |
786 ms |
28396 KB |
Output is correct |
49 |
Correct |
802 ms |
30444 KB |
Output is correct |
50 |
Correct |
789 ms |
31592 KB |
Output is correct |
51 |
Correct |
10 ms |
9728 KB |
Output is correct |
52 |
Correct |
530 ms |
21736 KB |
Output is correct |
53 |
Correct |
576 ms |
21480 KB |
Output is correct |
54 |
Correct |
551 ms |
21480 KB |
Output is correct |
55 |
Correct |
574 ms |
21612 KB |
Output is correct |
56 |
Correct |
578 ms |
21612 KB |
Output is correct |
57 |
Correct |
551 ms |
21480 KB |
Output is correct |
58 |
Correct |
561 ms |
24476 KB |
Output is correct |
59 |
Correct |
586 ms |
24680 KB |
Output is correct |
60 |
Correct |
804 ms |
24520 KB |
Output is correct |
61 |
Correct |
745 ms |
24300 KB |
Output is correct |
62 |
Correct |
298 ms |
33004 KB |
Output is correct |
63 |
Correct |
284 ms |
33000 KB |
Output is correct |
64 |
Correct |
293 ms |
32748 KB |
Output is correct |
65 |
Correct |
280 ms |
33004 KB |
Output is correct |
66 |
Correct |
410 ms |
27500 KB |
Output is correct |
67 |
Correct |
409 ms |
27368 KB |
Output is correct |
68 |
Correct |
416 ms |
27504 KB |
Output is correct |
69 |
Correct |
412 ms |
27500 KB |
Output is correct |
70 |
Correct |
412 ms |
27500 KB |
Output is correct |
71 |
Correct |
412 ms |
28908 KB |
Output is correct |
72 |
Correct |
432 ms |
30700 KB |
Output is correct |
73 |
Correct |
437 ms |
30444 KB |
Output is correct |
74 |
Correct |
425 ms |
31260 KB |
Output is correct |
75 |
Correct |
413 ms |
30952 KB |
Output is correct |
76 |
Correct |
653 ms |
30568 KB |
Output is correct |
77 |
Correct |
664 ms |
29676 KB |
Output is correct |
78 |
Correct |
818 ms |
31852 KB |
Output is correct |
79 |
Correct |
788 ms |
30572 KB |
Output is correct |
80 |
Correct |
817 ms |
32044 KB |
Output is correct |
81 |
Correct |
791 ms |
29168 KB |
Output is correct |
82 |
Correct |
795 ms |
29800 KB |
Output is correct |
83 |
Correct |
786 ms |
27244 KB |
Output is correct |
84 |
Correct |
786 ms |
30808 KB |
Output is correct |
85 |
Correct |
790 ms |
29808 KB |
Output is correct |
86 |
Correct |
780 ms |
26984 KB |
Output is correct |
87 |
Correct |
805 ms |
28144 KB |
Output is correct |
88 |
Correct |
718 ms |
28776 KB |
Output is correct |
89 |
Correct |
699 ms |
26220 KB |
Output is correct |
90 |
Correct |
706 ms |
26088 KB |
Output is correct |
91 |
Correct |
684 ms |
27624 KB |
Output is correct |
92 |
Correct |
720 ms |
26808 KB |
Output is correct |
93 |
Correct |
710 ms |
26600 KB |
Output is correct |
94 |
Correct |
683 ms |
26196 KB |
Output is correct |
95 |
Correct |
690 ms |
27112 KB |
Output is correct |
96 |
Correct |
704 ms |
26344 KB |
Output is correct |
97 |
Correct |
672 ms |
27624 KB |
Output is correct |