# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
852915 | Trisanu_Das | 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 <bits/stdc++.h>
using namespace std;
int n,f[1000005][2][2],C[1000005];
vector <int> G[1000005];
void dfs(int u, int p){
for(int i = 0; i <= 1; i++){
f[u][i][C[u]^i] = i;
f[u][i][C[u]^i^1] = n + 1;
}
for(int v : G[u])if(v!=fa){
dfs(v,u);
for(int i = 0; i <= 1; i++){
int f0=f[u][i][0],f1=f[u][i][1];
f[u][i][0]=min(n+1,min(f0+f[v][0][i],f1+f[v][1][i]));
f[u][i][1]=min(n+1,min(f0+f[v][1][i],f1+f[v][0][i]));
}
}
}
int main(){
cin >> n;
for(int i = 2; i <= n; i++){
int u,v;
cin >> u >> v;
G[u].push_back(v),G[v].push_back(u);
}
rep(i,1,n)cin >> C[i];
dfs(1,0);
int ans=min(f[1][0][0],f[1][1][0]);
if(ans>n) cout << "impossible\n";
else cout << ans << '\n';
}