제출 #1028446

#제출 시각아이디문제언어결과실행 시간메모리
1028446mareksbThe Xana coup (BOI21_xanadu)C++14
100 / 100
38 ms13508 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5+5; int dp[N][2][2];//(node,node color,toggle or no) vector<int> mas[N]; bool color[N]; void setdp(int v,bool toggle){ dp[v][color[v]^toggle][toggle]=toggle; //impossible dp[v][color[v]^toggle][!toggle]=N; } void updatedp(int v,int u, bool toggle){ int b=dp[v][0][toggle]; int w=dp[v][1][toggle]; dp[v][0][toggle]=min(b+dp[u][toggle][0],w+dp[u][toggle][1]); dp[v][1][toggle]=min(b+dp[u][toggle][1],w+dp[u][toggle][0]); } void dfs(int v, int p){ setdp(v,1); setdp(v,0); for(auto u:mas[v]){ if(u==p)continue; dfs(u,v); updatedp(v,u,0); updatedp(v,u,1); } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; for(int i=0;i<n-1;i++){ int x,y; cin>>x>>y; mas[x].push_back(y); mas[y].push_back(x); } for(int i=1;i<=n;i++){ cin>>color[i]; } dfs(1,1); int ans=min(dp[1][0][0],dp[1][0][1]); if(ans>n){ cout<<"impossible"; return 0; } 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...