Submission #1023397

#TimeUsernameProblemLanguageResultExecution timeMemory
1023397vjudge1The Xana coup (BOI21_xanadu)C++17
100 / 100
34 ms17236 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << endl
#define all(x) x.begin(),x.end()

vector<int> adj[100010];
ll dp[100010][2][2];
bool a[100010];

void dfs(int u,int p)
{
	if(a[u])
	{
		dp[u][0][0]=1e9;
		dp[u][0][1]=1;
		dp[u][1][0]=0;
		dp[u][1][1]=1e9;
	}
	else
	{
		dp[u][0][0]=0;
		dp[u][0][1]=1e9;
		dp[u][1][0]=1e9;
		dp[u][1][1]=1;
	}
	for(auto i : adj[u])
	{
		if(i!=p)
		{
			dfs(i,u);
			ll x[4]={dp[u][0][0],dp[u][0][1],dp[u][1][0],dp[u][1][1]};
			dp[u][0][0]=min(dp[i][0][0]+x[0],dp[i][0][1]+x[2]);
			dp[u][0][1]=min(dp[i][1][0]+x[1],dp[i][1][1]+x[3]);
			dp[u][1][0]=min(dp[i][0][0]+x[2],dp[i][0][1]+x[0]);
			dp[u][1][1]=min(dp[i][1][0]+x[3],dp[i][1][1]+x[1]);
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	int n;cin>>n;
	for(int i=1;i<n;i++)
	{
		int u,v;cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(int i=1;i<=n;i++) cin>>a[i];
	dfs(1,-1);
	ll ans=min(dp[1][0][0],dp[1][0][1]);
	if(ans>=1e9) cout<<"impossible";
	else cout<<ans;
	return 0;
}
#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...