Submission #1128509

#TimeUsernameProblemLanguageResultExecution timeMemory
1128509Alihan_8The Xana coup (BOI21_xanadu)C++20
100 / 100
112 ms39032 KiB
#include <bits/stdc++.h> using namespace std; using i64 = long long; void chmin(int &x, const int &y){ x = min(x, y); } int dp[100005][2][2]; signed main(){ int n; cin >> n; vector <vector<int>> adj(n); for ( int i = 1; i < n; i++ ){ int u, v; cin >> u >> v; --u, --v; adj[u].push_back(v); adj[v].push_back(u); } vector <int> a(n); for ( auto &u: a ) cin >> u; for ( int i = 0; i < n; i++ ){ for ( int j = 0; j < 2; j++ ){ dp[i][j][0] = dp[i][j][1] = n + 1; } } auto dfs = [&](auto dfs, int u, int p) -> void{ vector <int> x(2, n + 1), y(2, n + 1); x[0] = y[0] = 0; for ( auto &v: adj[u] ){ if ( v != p ){ dfs(dfs, v, u); vector <int> xx(2), yy(2); for ( auto k: {0, 1} ){ xx[k] = min(dp[v][0][k] + x[0], dp[v][0][k ^ 1] + x[1]); yy[k] = min(dp[v][1][k] + y[0], dp[v][1][k ^ 1] + y[1]); } swap(xx, x), swap(yy, y); } } for ( auto k: {0, 1} ){ chmin(dp[u][a[u] ^ k][0], x[k]); chmin(dp[u][a[u] ^ k ^ 1][1], y[k] + 1); } }; dfs(dfs, 0, -1); int ans = min(dp[0][0][1], dp[0][0][0]); if ( ans == n + 1 ) return cout << "impossible\n", 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...