제출 #867079

#제출 시각아이디문제언어결과실행 시간메모리
867079HossamHero7The Xana coup (BOI21_xanadu)C++14
55 / 100
71 ms25024 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' vector<vector<int>> adj; vector<int> on; const int N = 1e5+5; ll dp[N][3][2]; ll solve(int node,int par,int pressed,bool color){ ll &ret = dp[node][pressed][color]; if(~ret) return ret; ret = 1e9; if(pressed != 1){ int sz = adj[node].size(); if(node) sz --; for(int mask=0;mask<(1<<sz);mask++){ bool changed = __builtin_popcount(mask) & 1; if((color^changed)) continue; ll c = 0; int idx = 0; for(auto ch : adj[node]){ if(ch == par) continue; if(((mask>>idx)&1)) c += solve(ch,node,1,on[ch]^1) + 1; else c += solve(ch,node,0,on[ch]); idx ++; } ret = min(ret,c); } } if(pressed != 0){ int sz = adj[node].size(); if(node) sz --; for(int mask=0;mask<(1<<sz);mask++){ bool changed = __builtin_popcount(mask) & 1; if(pressed == 1 && (color^changed)) continue; if(pressed == 2 && !((color^changed))) continue; ll c = 0; int idx = 0; for(auto ch : adj[node]){ if(ch == par) continue; if(((mask>>idx)&1)) c += solve(ch,node,1,on[ch]) + 1; else c += solve(ch,node,0,on[ch]^1); idx ++; } if(pressed == 2) c ++; ret = min(ret,c); } } return ret; } void solve(){ int n; cin>>n; adj.resize(n); for(int i=0;i<n-1;i++){ int a,b; cin>>a>>b; a -- , b --; adj[a].push_back(b); adj[b].push_back(a); } on.resize(n); for(int i=0;i<n;i++){ int x;cin>>x; if(x) on[i] = 1; } memset(dp,-1,sizeof(dp)); ll ans = solve(0,n,2,on[0]); if(ans >= 1e9) cout<<"impossible"<<endl; else cout<<ans<<endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } 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...