#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 1e5;
int dp[N][2][2], a[N];
std::vector<int> e[N];
void dfs(int u, int p) {
if (e[u].size() == 1 && p != -1) {
if (a[u] == 0) {
dp[u][0][0] = 0;
dp[u][1][1] = 1;
} else {
dp[u][1][0] = 0;
dp[u][0][1] = 1;
}
return;
}
std::vector<int> c;
for (auto v : e[u]) {
if (v == p) continue;
dfs(v, u);
c.push_back(v);
}
int k = c.size();
if (k == 1) {
int l = c[0];
if (a[u] == 0) {
dp[u][0][0] = dp[l][0][0];
dp[u][0][1] = dp[l][1][1] + 1;
dp[u][1][0] = dp[l][0][1];
dp[u][1][1] = dp[l][1][0] + 1;
} else {
dp[u][1][0] = dp[l][1][0];
dp[u][1][1] = dp[l][1][1] + 1;
dp[u][0][0] = dp[l][0][1];
dp[u][0][1] = dp[l][1][0] + 1;
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
memset(dp, 0x3f, sizeof dp);
dfs(0, -1);
int ans = 1e9;
for (int j = 0; j < 2; j++) {
ans = std::min(ans, dp[0][0][j]);
}
if (ans == 1e9) {
std::cout << "impossible\n";
} else {
std::cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
84 ms |
18500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
74 ms |
18604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |