제출 #677171

#제출 시각아이디문제언어결과실행 시간메모리
677171amirhoseinfar1385The Xana coup (BOI21_xanadu)C++17
100 / 100
86 ms22212 KiB
#include<bits/stdc++.h> using namespace std; vector<vector<long long>>adj; long long n; const int maxn=100000+5; long long dp[2][2][maxn],fake[2][2][maxn]; vector<long long>all; long long inf=1e9+5; void solve(long long u=1,long long par=0){ int cnt=0; if(all[u]==1){ dp[0][1][u]=0; dp[1][0][u]=1; dp[0][0][u]=inf; dp[1][1][u]=inf; } else{ dp[0][0][u]=0; dp[1][1][u]=1; dp[0][1][u]=inf; dp[1][0][u]=inf; } for(auto x:adj[u]){ if(x!=par){ cnt++; solve(x,u); fake[0][0][u]=min(dp[0][0][u]+dp[0][0][x],dp[0][1][u]+dp[1][0][x]); fake[0][1][u]=min(dp[0][1][u]+dp[0][0][x],dp[0][0][u]+dp[1][0][x]); fake[1][0][u]=min(dp[1][0][u]+dp[0][1][x],dp[1][1][u]+dp[1][1][x]); fake[1][1][u]=min(dp[1][1][u]+dp[0][1][x],dp[1][0][u]+dp[1][1][x]); dp[0][0][u]=fake[0][0][u]; dp[1][0][u]=fake[1][0][u]; dp[0][1][u]=fake[0][1][u]; dp[1][1][u]=fake[1][1][u]; } } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; all.resize(n+1); adj.resize(n+1); for(long long i=1;i<=n-1;i++){ long long u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); } for(long long i=1;i<=n;i++){ cin>>all[i]; } solve(1); long long res=min(dp[1][0][1],dp[0][0][1]); if(res>=1e9+5){ cout<<"impossible"<<endl; return 0; } cout<<res<<endl; }
#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...