# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
506384 | Elegia | The Xana coup (BOI21_xanadu) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cmath>
#include <algorithm>
#include <bitset>
#include <numeric>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
using ull = unsigned long long;
using namespace std;
const int P = 998244353;
int norm(int x) { return x >= P ? x - P : x; }
int reduce(int x) { return x < 0 ? x + P : x; }
int neg(int x) { return x ? P - x : 0; }
void add(int& x, int y) { if ((x += y - P) < 0) x += P; }
void sub(int& x, int y) { if ((x -= y) < 0) x += P; }
void fam(int& x, int y, int z) { x = (x + y * (ull)z) % P; }
int mpow(int x, unsigned k) {
if (k == 0) return 1;
int ret = mpow(x * (ull)x % P, k >> 1);
if (k & 1) ret = ret * (ull)x % P;
return ret;
}
int quo2(int x) { return ((x & 1) ? x + P : x) >> 1; }
const int _ = 100010;
vector<int> G[_];
int f[_], dp[_][2][2];
void dfs(int u, int p) {
dp[u][0][f[u]] = 0; dp[u][1][f[u] ^ 1] = 1;
dp[u][0][f[u] ^ 1] = dp[u][1][f[u]] = _;
for (int v : G[u]) if (p != v) {
dfs(v, u);
static int tmp[2][2];
for (int a = 0; a <= 1; ++a)
for (int b = 0; b <= 1; ++b) {
tmp[a][b] = _;
for (int c = 0; c <= 1; ++c)
tmp[a][b] = min(tmp[a][b], dp[u][a][b ^ c] + dp[v][c][a]);
}
memcpy(dp[u], tmp, sizeof(tmp));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int N; cin >> N;
for (int rep = 1; rep != N; ++rep) {
int u, v; cin >> u >> v;
G[u].push_back(v); G[v].push_back(u);
}
for (int i = 1; i <= N; ++i) cin >> f[i];
dfs(1, 0);
int ans = min(dp[1][0][0], dp[1][1][0]);
if (ans <= N) cout << ans << '\n';
else cout << "impossible\n";
return 0;
}