제출 #677073

#제출 시각아이디문제언어결과실행 시간메모리
677073ajab_01The Xana coup (BOI21_xanadu)C++17
100 / 100
74 ms19192 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int N = 1e5 + 5; const int INF = 1e12; vector<int> g[N]; int dp[N][2][2] , sit[N] , root , n; void dfs(int ver , int par){ for(int i : g[ver]){ if(i == par) continue; dfs(i , ver); } if((int)g[ver].size() == 1){ dp[ver][sit[ver]][0] = 0; dp[ver][sit[ver] ^ 1][1] = 1; return; } int sum1 = 0 , sum2 = 0 , cnt1 = 0 , cnt2 = 0 , rem1 = INF , rem2 = INF; for(int i : g[ver]){ if(i == par) continue; sum1 += min(dp[i][0][1] , dp[i][0][0]); sum2 += min(dp[i][1][1] , dp[i][1][0]); rem1 = min(rem1 , max(dp[i][0][1] , dp[i][0][0]) - min(dp[i][0][1] , dp[i][0][0])); rem2 = min(rem2 , max(dp[i][1][1] , dp[i][1][0]) - min(dp[i][1][1] , dp[i][1][0])); if(dp[i][0][1] <= dp[i][0][0]) cnt1++; if(dp[i][1][1] <= dp[i][1][0]) cnt2++; } for(int k = 0 ; k < 2 ; k++){ if(k == 1){ dp[ver][sit[ver]][k] = min(dp[ver][sit[ver]][k] , sum2 + (cnt2 & 1 ? 0 : rem2) + 1); dp[ver][sit[ver] ^ 1][k] = min(dp[ver][sit[ver] ^ 1][k] , sum2 + (cnt2 & 1 ? rem2 : 0) + 1); } else{ dp[ver][sit[ver]][k] = min(dp[ver][sit[ver]][k] , sum1 + (cnt1 & 1 ? rem1 : 0)); dp[ver][sit[ver] ^ 1][k] = min(dp[ver][sit[ver] ^ 1][k] , sum1 + (cnt1 & 1 ? 0 : rem1)); } } } int32_t main(){ ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for(int i = 0 ; i < n - 1 ; i++){ int u , v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); if((int)g[v].size() > 1) root = v; if((int)g[u].size() > 1) root = u; } for(int i = 0 ; i < n ; i++) cin >> sit[i]; for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < 2 ; j++) for(int k = 0 ; k < 2 ; k++) dp[i][j][k] = INF; dfs(root , -1); int ans = min(dp[root][0][1] , dp[root][0][0]); if(ans > n) cout << "impossible" << '\n'; else cout << ans << '\n'; 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...