#include "bits/stdc++.h"
using namespace std;
#ifdef duc_debug
#include "bits/debug.h"
#else
#define debug(...)
#endif
const int maxn = 1e5 + 5;
int n, a[maxn];
int f[maxn][2][2];
vector<int> g[maxn];
int ff[2][2];
void dfs(int u, int prev) {
f[u][a[u]][0] = 0;
f[u][a[u] ^ 1][1] = 1;
for (auto v : g[u]) {
if (v == prev) continue;
dfs(v, u);
memset(ff, 0x3f, sizeof(ff));
for (int i = 0; i < 2; ++i) {
ff[i][0] = min(ff[i][0], f[u][i ^ 1][0] + f[v][0][1]);
ff[i][0] = min(ff[i][0], f[u][i][0] + f[v][0][0]);
ff[i][1] = min(ff[i][1], f[u][i][1] + f[v][1][0]);
ff[i][1] = min(ff[i][1], f[u][i ^ 1][1] + f[v][1][1]);
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
f[u][i][j] = ff[i][j];
}
}
}
// for (int i = 0; i < 2; ++i) {
// for (int j = 0; j < 2; ++j) {
// debug(u, i, j, f[u][i][j]);
// }
// }
}
void solve() {
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
memset(f, 0x3f, sizeof(f));
dfs(1, 0);
int res = min(f[1][0][0], f[1][0][1]);
if (res > 1e9) {
cout << "impossible\n";
return;
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |