제출 #1097744

#제출 시각아이디문제언어결과실행 시간메모리
1097744vjudge1The Xana coup (BOI21_xanadu)C++17
100 / 100
76 ms26708 KiB
#include<bits/stdc++.h> #define int long long using namespace std; const int maxn=2e5+10,inf=0x3f3f3f3f; int n,a[maxn],f[maxn][3][3],g1[maxn][3],g2[maxn][3]; vector<int>e[maxn]; void dfs(int u,int fa){ if(e[u].size()==1&&u!=1){ if(a[u]==1){ f[u][0][1]=0; f[u][1][0]=1; } else{ f[u][0][0]=0; f[u][1][1]=1; } return ; } for(int v:e[u]){ if(v==fa)continue; dfs(v,u); } g1[0][1]=g2[0][1]=inf; int cnt=0; for(int v:e[u]){ if(v==fa)continue; ++cnt; g1[cnt][0]=min(g1[cnt-1][1]+f[v][1][0],g1[cnt-1][0]+f[v][0][0]); g1[cnt][1]=min(g1[cnt-1][1]+f[v][0][0],g1[cnt-1][0]+f[v][1][0]); g2[cnt][0]=min(g2[cnt-1][1]+f[v][1][1],g2[cnt-1][0]+f[v][0][1]); g2[cnt][1]=min(g2[cnt-1][1]+f[v][0][1],g2[cnt-1][0]+f[v][1][1]); } if(a[u]==0){ f[u][0][0]=g1[cnt][0]; f[u][0][1]=g1[cnt][1]; f[u][1][0]=g2[cnt][1]+1; f[u][1][1]=g2[cnt][0]+1; } else if(a[u]==1){ f[u][0][0]=g1[cnt][1]; f[u][0][1]=g1[cnt][0]; f[u][1][0]=g2[cnt][0]+1; f[u][1][1]=g2[cnt][1]+1; } } signed main(){ cin>>n; for(int i=1;i<=n;i++)f[i][1][1]=f[i][1][0]=f[i][0][1]=f[i][0][0]=inf; for(int i=1;i<n;i++){ int u,v; cin>>u>>v; e[u].emplace_back(v); e[v].emplace_back(u); } for(int i=1;i<=n;i++)cin>>a[i]; dfs(1,0); if(f[1][1][0]>=inf&&f[1][0][0]>=inf)cout<<"impossible"; else cout<<min(f[1][0][0],f[1][1][0]); 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...