#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define ff first
#define ss second
#define eack emplace_back
#define all(x) (x).begin(), (x).end()
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[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:38: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 |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
832 ms |
32888 KB |
Output is correct |
2 |
Correct |
280 ms |
33128 KB |
Output is correct |
3 |
Correct |
812 ms |
32624 KB |
Output is correct |
4 |
Correct |
281 ms |
33132 KB |
Output is correct |
5 |
Incorrect |
809 ms |
30828 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Correct |
10 ms |
9728 KB |
Output is correct |
3 |
Correct |
10 ms |
9856 KB |
Output is correct |
4 |
Correct |
10 ms |
9856 KB |
Output is correct |
5 |
Correct |
10 ms |
9856 KB |
Output is correct |
6 |
Correct |
10 ms |
9856 KB |
Output is correct |
7 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |