Submission #1017722

#TimeUsernameProblemLanguageResultExecution timeMemory
1017722vjudge1The Xana coup (BOI21_xanadu)C++17
100 / 100
50 ms33092 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define int long long #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORD(i, a, b) for (int i = (b); i >= (a); i --) #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPD(i, n) for (int i = (n) - 1; i >= 0; --i) #define MASK(i) (1LL << (i)) #define BIT(x, i) (((x) >> (i)) & 1) constexpr ll LINF = (1ll << 60); constexpr int INF = (1ll << 30); constexpr int Mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* Phu Trong from Nguyen Tat Thanh High School for gifted student */ template <class X, class Y> bool minimize(X &x, const Y &y){ X eps = 1e-9; if (x > y + eps) {x = y; return 1;} return 0; } template <class X, class Y> bool maximize(X &x, const Y &y){ X eps = 1e-9; if (x + eps < y) {x = y; return 1;} return 0; } #define MAX 200005 int numNode; vector<int> G[MAX]; int label[MAX]; /* 1. It is optimal to press a button at most once 2. The pressing order is not important */ int dp[MAX][2][2]; void dfs(int u, int p = -1){ vector<int> store[2]; int sm_on = 0, sm_off = 0; for (int&v : G[u]) if(v ^ p){ dfs(v, u); store[0].push_back(dp[v][0][1] - dp[v][0][0]); store[1].push_back(dp[v][1][1] - dp[v][1][0]); sm_off += dp[v][0][0]; sm_on += dp[v][1][0]; } REP(i, 2) sort(store[i].begin(), store[i].end()); int min_odd = numNode + 1, min_even = 0, sm = 0; for (int i = 0; i < (int)store[0].size(); ++i){ sm += store[0][i]; if ((i + 1) & 1) minimize(min_odd, sm); else minimize(min_even, sm); } minimize(dp[u][label[u]][0], min_even + sm_off); minimize(dp[u][label[u] ^ 1][0], min_odd + sm_off); min_odd = numNode + 1, min_even = 0, sm = 0; for (int i = 0; i < (int)store[1].size(); ++i){ sm += store[1][i]; if ((i + 1) & 1) minimize(min_odd, sm); else minimize(min_even, sm); } minimize(dp[u][label[u]][1], min_odd + sm_on + 1); minimize(dp[u][label[u] ^ 1][1], min_even + sm_on + 1); } void process(void){ cin >> numNode; for (int i = 1; i < numNode; ++i){ int u, v; cin >> u >> v; G[u].emplace_back(v); G[v].emplace_back(u); } for (int i = 1; i <= numNode; ++i) cin >> label[i]; for (int i = 1; i <= numNode; ++i) { REP(on, 2) REP(press, 2) dp[i][on][press] = numNode + 1; } dfs(1); int ans = min(dp[1][0][0], dp[1][0][1]); if(ans > numNode) return void(cout << "impossible"); cout << ans; } signed main(){ #define name "Whisper" cin.tie(nullptr) -> sync_with_stdio(false); //freopen(name".inp", "r", stdin); //freopen(name".out", "w", stdout); process(); }
#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...