제출 #987175

#제출 시각아이디문제언어결과실행 시간메모리
987175batsukh2006The Xana coup (BOI21_xanadu)C++17
50 / 100
54 ms19036 KiB
#include<iostream> #include<stdio.h> #include<math.h> #include<map> #include<string> #include<algorithm> #include<vector> #include<string.h> #include<utility> #include<set> #include<cmath> #include<queue> #include<deque> #include<functional> #include<stack> #include<limits.h> #include<iomanip> #include<unordered_map> #include<numeric> #include<tuple> #include<bitset> using namespace std; #define MOD 1000000007 #define int long long #define ff first #define ss second #define endl '\n' int st=-1; const int mxN=1e5+1; int dp[mxN][2][2]; vector<int> v[mxN],s(mxN); void dfs(int a, int p){ for(auto node: v[a]){ if(node!=p){ dfs(node,a); } } for(int i=0; i<2; i++){ for(int j=0; j<2; j++){ bool k=0; int c=s[a]^(i^j); pair<int,int> need; int cnt=v[a].size(); if(a!=st) cnt--; for(auto node: v[a]){ if(node!=p){ k^=1; if(cnt==1){ if(c==1) need={dp[node][1][i],1e9}; else need={dp[node][0][i],1e9}; }else{ if(c==1){ if(k==1){ need.ff+=dp[node][0][i]; need.ss+=dp[node][1][i]; }else{ need.ff+=dp[node][1][i]; need.ss+=dp[node][0][i]; } }else{ need.ff+=dp[node][0][i]; need.ss+=dp[node][1][i]; } } }else if(cnt==0){ if(c==1) need={1e9,1e9}; else need={0,0}; } } dp[a][i][j]=min(need.ff,need.ss)+i; } } } void solve(){ int n; cin>>n; for(int i=1; i<n; i++){ int a,b; cin>>a>>b; v[a].push_back(b); v[b].push_back(a); } for(int i=1; i<=n; i++){ cin>>s[i]; if(v[i].size()<=2) st=i; } dfs(st,-1); int ans=min(dp[st][0][0],dp[st][1][0]); if(ans>=1e9) cout<<"impossible"; else cout<<ans; } signed main(){ // freopen("file.in", "r", stdin); // freopen("file.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; // cin>>t; while(t--){ solve(); cout<<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...