제출 #467072

#제출 시각아이디문제언어결과실행 시간메모리
467072MVP_HarryThe Xana coup (BOI21_xanadu)C++17
100 / 100
100 ms21984 KiB
#include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define rep(i, m, n) for (int i = m; i <= n; i++) #define per(i, m, n) for (int i = m; i >= n; i--) #define sz(v) (int) v.size() #define all(v) v.begin(), v.end() #define ll long long #define pb push_back #define mp make_pair #define fi first #define se second const int maxn = 1e5 + 5; int N; ll dp[maxn][2][2], a[maxn]; vector<int> G[maxn]; void dfs(int u, int k) { ll sum1 = 0, sum2 = 0, mn1 = 1e18, mn2 = 1e18, cnt1 = 0, cnt2 = 0; for (auto v : G[u]) { if (v == k) continue; dfs(v, u); sum1 += min(dp[v][0][0], dp[v][0][1]); mn1 = min(mn1, abs(dp[v][0][0] - dp[v][0][1])); if (dp[v][0][1] < dp[v][0][0]) cnt1++; sum2 += min(dp[v][1][0], dp[v][1][1]); mn2 = min(mn2, abs(dp[v][1][0] - dp[v][1][1])); if (dp[v][1][1] < dp[v][1][0]) cnt2++; } if ((cnt1 + a[u]) % 2 == 0) { dp[u][0][0] = min(dp[u][0][0], sum1); dp[u][1][0] = min(dp[u][1][0], sum1 + mn1); } else { dp[u][0][0] = min(dp[u][0][0], sum1 + mn1); dp[u][1][0] = min(dp[u][1][0], sum1); } if ((cnt2 + a[u]) % 2 == 1) { dp[u][0][1] = min(dp[u][0][1], sum2 + 1); dp[u][1][1] = min(dp[u][1][1], sum2 + mn2 + 1); } else { dp[u][0][1] = min(dp[u][0][1], sum2 + mn2 + 1); dp[u][1][1] = min(dp[u][1][1], sum2 + 1); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; rep(i, 1, N - 1) { int u, v; cin >> u >> v; G[u].pb(v), G[v].pb(u); } rep(i, 1, N) cin >> a[i]; rep(i, 1, N) rep(j, 0, 1) rep(k, 0, 1) dp[i][j][k] = 1e9; dfs(1, 0); ll ans = min(dp[1][0][0], dp[1][0][1]); if (ans == (ll) 1e9) { 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...