제출 #676942

#제출 시각아이디문제언어결과실행 시간메모리
676942VaheThe Xana coup (BOI21_xanadu)C++17
50 / 100
154 ms25752 KiB
#include <iostream> #include <vector> #include <set> #include <queue> #include <deque> #include <map> #include <stack> #include <algorithm> #include <cmath> #include <iomanip> #include <climits> using namespace std; using ll = long long; int n; vector<int> clr; vector<vector<int>> gp; vector<vector<vector<int>>> dp; void DPcreator(int u, int p, int nerk) { multiset<pair<int, int>> st; bool impossible = 0; for (int& v : gp[u]) { if (v == p) continue; if (dp[v][0][nerk] < 2e9) st.insert({ dp[v][0][nerk], 0 }); if (dp[v][1][nerk] < 2e9) st.insert({ dp[v][1][nerk], 1 }); if (dp[v][0][nerk] >= 2e9 && dp[v][1][nerk] >= 2e9) impossible = 1; } int sum = 0, cnt = clr[u] + nerk, q = gp[u].size() - (p != -1); int f0 = -1, f1 = -1, s0 = -1, s1 = -1; for (pair<int, int> i : st) { if (q) { q--; cnt += i.second; sum += i.first; i.second ? (f1 = i.first) : (f0 = i.first); } else { if (i.second && s1 == -1) s1 = i.first; if (!i.second && s0 == -1) s0 = i.first; } } int NotChanged = sum + nerk; // Changing... if ((f0 == -1 || s1 == -1) && (f1 == -1 || s0 == -1)) sum = 2e9; else if (f0 != -1 && s1 != -1 && f1 != -1 && s0 != -1) sum += min(s1 - f0, s0 - f1); else if (f0 == -1 || s1 == -1) sum += s0 - f1; else sum += s1 - f0; int Changed = sum + nerk; // if (impossible) NotChanged = Changed = 2e9; // if (cnt & 1) dp[u][nerk][0] = Changed, dp[u][nerk][1] = NotChanged; else dp[u][nerk][0] = NotChanged, dp[u][nerk][1] = Changed; } void dfs(int u, int p) { if (gp[u].size() == 1 && u != 0) { if (clr[u]) { dp[u][0][0] = 2e9; dp[u][0][1] = 0; dp[u][1][0] = 1; dp[u][1][1] = 2e9; } else { dp[u][0][0] = 0; dp[u][0][1] = 2e9; dp[u][1][0] = 2e9; dp[u][1][1] = 1; } return; } for (int& v : gp[u]) { if (v == p) continue; if (dp[v][0][0] == -1) dfs(v, u); } DPcreator(u, p, 0); DPcreator(u, p, 1); } int main() { int n; cin >> n; gp = vector<vector<int>>(n); dp = vector<vector<vector<int>>>(n, { {-1, -1}, {-1, -1} }); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; gp[--u].push_back(--v); gp[v].push_back(u); } clr = vector<int>(n); for (int& i : clr) cin >> i; dfs(0, -1); int ans = min(dp[0][0][0], dp[0][1][0]); if (ans >= 2e9) cout << "impossible\n"; else cout << ans << 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...