Submission #583502

#TimeUsernameProblemLanguageResultExecution timeMemory
583502WongChun1234The Xana coup (BOI21_xanadu)C++14
100 / 100
146 ms27484 KiB
#include<bits/stdc++.h> using namespace std; const int N=100050; int n,u,v; long long dp[2][2][N]; bool st[N]; vector<int> adj[N]; void dfs(int nde,int par=-1){ //flip now? is on? bool pt=1; long long curr[2][2][2]={{{0,0},{0,0}},{{INT_MAX,INT_MAX},{INT_MAX,INT_MAX}}}; //chosen%2 flip this for (int i:adj[nde]){ if (i==par) continue; dfs(i,nde); curr[0][0][pt]=min(curr[0][0][!pt]+dp[0][0][i],curr[1][0][!pt]+dp[1][0][i]); curr[0][1][pt]=min(curr[0][1][!pt]+dp[0][1][i],curr[1][1][!pt]+dp[1][1][i]); curr[1][0][pt]=min(curr[1][0][!pt]+dp[0][0][i],curr[0][0][!pt]+dp[1][0][i]); curr[1][1][pt]=min(curr[1][1][!pt]+dp[0][1][i],curr[0][1][!pt]+dp[1][1][i]); //cerr<<nde<<"::"<<curr[0][0][pt]<<" "<<curr[0][1][pt]<<" "<<curr[1][0][pt]<<" "<<curr[1][1][pt]<<"\n"; pt^=1; } //no flip dp[0][0][nde]=curr[st[nde]][0][!pt]; dp[0][1][nde]=curr[!st[nde]][0][!pt]; //flip dp[1][0][nde]=curr[!st[nde]][1][!pt]+1; dp[1][1][nde]=curr[st[nde]][1][!pt]+1; //cout<<nde<<"::"<<st[nde]<<": noflip:"<<dp[0][0][nde]<<" "<<dp[0][1][nde]<<" flip:"<<dp[1][0][nde]<<" "<<dp[1][1][nde]<<"\n"; } int main(){ cin>>n; for (int i=1;i<n;i++){ cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } for (int i=1;i<=n;i++) cin>>st[i]; dfs(1); long long ans=min(dp[0][0][1],dp[1][0][1]); if (ans>=1e9) cout<<"impossible\n"; else cout<<ans<<"\n"; }
#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...