Submission #417302

#TimeUsernameProblemLanguageResultExecution timeMemory
417302BlagojceThe Xana coup (BOI21_xanadu)C++11
100 / 100
85 ms22488 KiB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
  
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;

mt19937 _rand(time(NULL));
const int mxn = 2e5;

int n;
vector<int> g[mxn];

int a[mxn];
int dp[mxn][2][2];


void dfs(int u, int p){
	int ver = -1;
	for(auto e : g[u]){
		if(e == p) continue;
		ver = e;
		dfs(e, u);
	}
	fr(t, 0, 2) fr(p, 0, 2) dp[u][t][p] = 1e9;
	
	if(ver == -1){
		
		if(a[u] == 0){
			dp[u][0][0] = 0;
			dp[u][1][1] = 1;
		}
		else{
			dp[u][0][1] = 1;
			dp[u][1][0] = 0;
		}
		return;
	}
	int D[2][2];
	int D2[2][2];
	fr(t, 0, 2){
		fr(i, 0, 2) D[t][i] = dp[ver][t][i];
		
		for(auto e : g[u]){
			if(e == p || e == ver) continue;
			
			fr(i, 0, 2) D2[t][i] = 1e9;
			fr(prv, 0, 2){
				fr(k, 0, 2){
					D2[t][prv^k] = min(D2[t][prv^k], D[t][prv] + dp[e][t][k]);
				}
			}
			fr(i, 0, 2) D[t][i] = D2[t][i];
		}
	}
	
	
	
	
	
	fr(t, 0, 2){
		fr(p, 0, 2){
			int rem = t^p^a[u];
			dp[u][t][p] = D[p][rem]+p;
		}
	}
	
	
	
	
}

void solve(){
	cin >> n;
	fr(i, 0, n-1){
		int u, v;
		cin >> u >> v;
		--u, --v;
		g[u].pb(v);
		g[v].pb(u);
	}
	fr(i, 0, n){
		cin >> a[i];
	}dfs(0, 0);
	int ans = min(dp[0][0][0], dp[0][0][1]);
	if(ans >= 1e9){
		cout<<"impossible"<<endl;
	}
	else{
		cout<<ans<<endl;
	}

}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	solve();
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...