Submission #1289000

#TimeUsernameProblemLanguageResultExecution timeMemory
1289000djawadmainThe Xana coup (BOI21_xanadu)C++20
100 / 100
37 ms20128 KiB
#include<iostream> #include<vector> using namespace std; #define ll long long #define ld long double #define pii pair<ll, ll> #define RUN_LIKE_DJAWAD (ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)); #define pb push_back #define pf push_front #define ff first #define ss second #define maxn 100001 vector<ll> g[maxn]; ll dp[maxn][2][2]; ll n, ui, vi; bool s[maxn], seen[maxn]; void dfs(ll v){ seen[v] = true; ll a0 = 0, d0 = 1e9, a1 = 0, d1 = 1e9; bool c0 = 0, c1 = 0; for (ll u: g[v]) if (not seen[u]){ dfs(u); a0 += min(dp[u][0][0], dp[u][0][1]); d0 = min(d0, abs(dp[u][0][0] - dp[u][0][1])); if (dp[u][0][1] < dp[u][0][0]) c0 ^= 1; a1 += min(dp[u][1][0], dp[u][1][1]); d1 = min(d1, abs(dp[u][1][0] - dp[u][1][1])); if (dp[u][1][1] < dp[u][1][0]) c1 ^= 1; } if (c0){ a0 += d0; d0 *= -1; } if (c1){ a1 += d1; d1 *= -1; } if (s[v]){ dp[v][0][0] = a0 + d0; dp[v][0][1] = a1 + 1; dp[v][1][0] = a0; dp[v][1][1] = a1 + d1 + 1; } else { dp[v][0][0] = a0; dp[v][0][1] = a1 + d1 + 1; dp[v][1][0] = a0 + d0; dp[v][1][1] = a1 + 1; } } int main(){ RUN_LIKE_DJAWAD cin >> n; for (ll i = 1; i < n; i++){ cin >> ui >> vi; g[ui].pb(vi); g[vi].pb(ui); } for (ll i = 1; i <= n; i++) cin >> s[i]; dfs(1); ll ans = min(dp[1][0][0], dp[1][0][1]); if (ans >= 1e9){ 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...