#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define D(x...) printf(x)
#else
#define D(x...)
#endif
#define MN 500100
#define x first
#define y second
int N, K, S[MN];
vector<int> adj[MN];
int tot[MN], cnt[MN], ans, dep[MN], par[MN];
bool bad[MN];
void dfs(int n, int p, int d) {
dep[n] = d;
par[n] = p;
int cur[8];
for (int i = 1; i <= K; i++) cur[i] = cnt[i];
cnt[S[n]]++;
for (int i : adj[n]) if (i != p) {
dfs(i, n, d+1);
}
for (int i = 1; i <= K; i++) cur[i] = cnt[i]-cur[i];
if (n == 1) return;
bool f = 1;
for (int i = 1; i <= K; i++) if (cur[i] && cur[i] != tot[i]) {
f = 0;
break;
}
bad[n] = f;
}
pair<int, int> dp[MN];
pair<int, int> bpos;
int bval;
void dfs2(int n, int p) {
pair<int, int> b1 = {0, 0}, b2 = {0, 0};
for (int i : adj[n]) if (i != p) {
dfs2(i, n);
if (dp[i].x >= b1.x) {
b2 = b1;
b1 = dp[i];
} else if (dp[i].x >= b2.x) {
b2 = dp[i];
}
}
if (b2.y && b1.x+b2.x > bval) {
bval = b1.x+b2.x;
bpos = {b1.y, b2.y};
}
if (b1.x == 0 && bad[n]) {
b1 = {1, n};
} else if (bad[n]) {
b1.x++;
}
dp[n] = b1;
}
void rem() {
int a = bpos.x, b = bpos.y;
if (dep[a] < dep[b]) swap(a, b);
while (dep[a] > dep[b]) {
bad[a] = 0;
a = par[a];
}
while (a != b) {
bad[a] = bad[b] = 0;
a = par[a]; b = par[b];
}
}
int main() {
scanf("%d%d", &N, &K);
assert(N <= 100);
for (int i = 1, A, B; i < N; i++) {
scanf("%d%d", &A, &B);
adj[A].push_back(B);
adj[B].push_back(A);
}
for (int i = 1; i <= N; i++) {
scanf("%d", &S[i]);
tot[S[i]]++;
}
dfs(1, 0, 0);
while (1) {
for (int j = 1; j <= N; j++) dp[j] = {0, 0};
bval = 0;
bpos = {0, 0};
dfs2(1, 0);
D("%d %d %d\n", bval, bpos.x, bpos.y);
if (!bval) {
if (dp[1].x) ans++;
break;
} else {
rem();
ans++;
}
}
printf("%d\n", (ans+1)/2);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &A, &B);
~~~~~^~~~~~~~~~~~~~~~
mergers.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &S[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
12160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
12160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
12160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
27 ms |
24028 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
12160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |