Submission #568119

#TimeUsernameProblemLanguageResultExecution timeMemory
568119inluminasThe Xana coup (BOI21_xanadu)C++17
100 / 100
89 ms15052 KiB
#include"bits/stdc++.h" using namespace std; #define ll long long #define endl "\n" #define fastio ios_base::sync_with_stdio(false) #define inf LLONG_MAX #define l first #define r second const ll lmt=1e5+10; vector<ll>adj[lmt]; bool on[lmt]; ll dp[lmt][2][2]; void check(ll u,ll p,ll ver){ bool tata=0; ll val=0,one=0; vector<ll>a; for(ll v:adj[u]){ if(v==p) continue; if(dp[v][ver][1]==1e15 && dp[v][ver][0]==1e15){ tata=1; break; }else if(dp[v][ver][0]==1e15){ val+=dp[v][ver][1],one++; }else if(dp[v][ver][1]==1e15){ val+=dp[v][ver][0]; }else{ val+=dp[v][ver][0]; a.push_back(-dp[v][ver][0]+dp[v][ver][1]); } } if(tata) return; sort(a.begin(),a.end()); dp[u][on[u]^((one+ver)%2)][ver]=min(dp[u][on[u]^((one+ver)%2)][ver],val+ver); for(ll x:a){ val+=x,one++; dp[u][on[u]^((one+ver)%2)][ver]=min(dp[u][on[u]^((one+ver)%2)][ver],val+ver); } return; } void dfs(ll u,ll p){ ll deg=0; for(ll v:adj[u]){ if(v==p) continue; dfs(v,u); deg++; } if(!deg){ dp[u][on[u]][0]=0,dp[u][on[u]^1][1]=1; //cout<<dp[u][0][0]<<" "<<dp[u][0][1]<<" "<<dp[u][1][0]<<" "<<dp[u][1][1]<<endl; return; }else{ check(u,p,0); check(u,p,1); } } int main(){ fastio; ll n; cin>>n; for(ll i=1;i<n;i++){ ll u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } for(ll i=1;i<=n;i++){ bool x; cin>>x; on[i]=x; } for(ll i=0;i<=n;i++){ for(ll j=0;j<2;j++){ for(ll k=0;k<2;k++){ dp[i][j][k]=1e15; } } } dfs(1,-1); ll ans=min(dp[1][0][0],dp[1][0][1]); if(ans==1e15) cout<<"impossible"<<endl; else cout<<ans<<endl; 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...