제출 #677166

#제출 시각아이디문제언어결과실행 시간메모리
677166amirThe Xana coup (BOI21_xanadu)C++17
30 / 100
76 ms36356 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll ; typedef pair<int , int> pii ; typedef pair<ll , ll> pll ; #define ps push_back #define pb pop_back #define mp make_pair #define all(x) x.begin() , x.end() #define sz(x) (int)x.size() #define debug(a) cout<<"debug: "<<(#a)<<" = "<<a<<'\n' const ll maxn = 1e6 + 6 , inf = 1e8 +100 ; int dp[maxn][5] ; int col[maxn] ; int n ; vector<int > adj[maxn] ; void dfs(int v , int p = -1){ if (sz(adj[v]) == 1 && v != 1){ if (col[v]){ dp[v][2] = 0 ; dp[v][1] = 1 ; } else { dp[v][0] = 0 ; dp[v][3] = 1 ; } return ; } for (int u : adj[v]){ if (u != p){ dfs(u ,v) ; } } int mne = inf , sum = 0 ; int cnt1 = 0 ; for (int u : adj[v]){ if (u == p) continue ; if (dp[u][1] <= dp[u][0]){ cnt1 ++ ; sum += dp[u][1] ; mne = min(mne , (dp[u][0] - dp[u][1])) ; } else{ sum += dp[u][0] ; mne = min(mne , (dp[u][1] - dp[u][0])) ; } } /*debug(v) ; debug(sum) ; debug(cnt1) ; debug(mne) ; cout << '\n' ;*/ if (col[v]){ if (cnt1 % 2){ dp[v][0] = sum ; dp[v][2] = sum + mne ; } else { dp[v][2] = sum ; dp[v][0] = sum + mne ; } } else { if (cnt1 % 2){ dp[v][2] = sum ; dp[v][0] = sum + mne ; } else { dp[v][0] = sum ; dp[v][2] = sum + mne ; } } mne = inf , sum = 0 ; cnt1 = 0 ; for (int u : adj[v]){ if (u == p) continue ; if (dp[u][3] <= dp[u][2]){ cnt1 ++ ; sum += dp[u][3] ; mne = min(mne , (dp[u][2] - dp[u][3])) ; } else{ sum += dp[u][2] ; mne = min(mne , (dp[u][3] - dp[u][2])) ; } } if (col[v]){ if (cnt1 % 2){ dp[v][3] = sum +1; dp[v][1] = sum + mne +1; } else { dp[v][1] = sum +1; dp[v][3] = sum + mne +1; } } else { if (cnt1 % 2){ dp[v][1] = sum +1; dp[v][3] = sum + mne +1; } else { dp[v][3] = sum +1; dp[v][1] = sum + mne +1; } } return ; } int main() { ios::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ; cin >> n ; for (int i = 1 ; i <= n-1 ; i++){ int a , b ; cin >> a >> b ; adj[a].ps(b) ; adj[b].ps(a) ; } for (int i = 1 ;i <= n ; i++){ cin >> col[i] ; } for (int i = 1 ; i <= n ; i++){ for (int j = 0 ; j < 4 ; j++) dp[i][j] = inf ; } dfs(1) ; int ans = min(dp[1][0] , dp[1][1]) ; if (ans > n){ cout << "impossible\n" ; 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...