Submission #483004

#TimeUsernameProblemLanguageResultExecution timeMemory
483004MilosMilutinovicThe Xana coup (BOI21_xanadu)C++14
70 / 100
94 ms32868 KiB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

#define int ll
const int N=101000;
int n,a[N],dp[N][2][2];
VI g[N];
void dfs(int u,int p) {
	VI ch;
	for (int v:g[u]) {
		if (v==p) continue;
		dfs(v,u);
		ch.pb(v);
	}
	if (ch.empty()) {
		if (a[u]==0) dp[u][0][0]=0,dp[u][1][1]=1;
		else dp[u][1][0]=1,dp[u][0][1]=0;
	} else {
		vector<VI> ndp(SZ(ch),VI(2,1e9));
		rep(x,0,2) rep(y,0,2) {
			int st=(a[u]+x+y)%2;
			rep(i,0,SZ(ch)) {
				int v=ch[i];
				if (i==0) {
					ndp[i][0]=dp[v][0][x];
					ndp[i][1]=dp[v][1][x];
				} else {
					ndp[i][0]=min(ndp[i-1][0]+dp[v][0][x],ndp[i-1][1]+dp[v][1][x]);
					ndp[i][1]=min(ndp[i-1][1]+dp[v][0][x],ndp[i-1][0]+dp[v][1][x]);
				}
			}
			dp[u][x][y]=min(dp[u][x][y],ndp[SZ(ch)-1][st]+x);
		}
	}
}
signed main() {
	scanf("%lld",&n);
	rep(i,0,n-1) {
		int u,v;
		scanf("%lld%lld",&u,&v);
		g[u].pb(v);
		g[v].pb(u);
	}
	int root=1;
	rep(i,1,n+1) if (SZ(g[i])<=SZ(g[root])) root=i;
	rep(i,1,n+1) scanf("%lld",a+i);
	rep(i,1,n+1) rep(x,0,2) rep(y,0,2) dp[i][x][y]=1e9;
	dfs(root,0);
//	rep(i,1,n+1) {
//		rep(x,0,2) rep(y,0,2) printf("i:%d x:%d y:%d dp:%d\n",i,x,y,dp[i][x][y]);
//	}
	int ans=min(dp[root][1][0],dp[root][0][0]);
	if (ans>=1e9) printf("impossible");
	else printf("%lld\n",ans);
}

Compilation message (stderr)

xanadu.cpp: In function 'int main()':
xanadu.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%lld",&n);
      |  ~~~~~^~~~~~~~~~~
xanadu.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   scanf("%lld%lld",&u,&v);
      |   ~~~~~^~~~~~~~~~~~~~~~~~
xanadu.cpp:64:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |  rep(i,1,n+1) scanf("%lld",a+i);
      |               ~~~~~^~~~~~~~~~~~
#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...