Submission #974147

#TimeUsernameProblemLanguageResultExecution timeMemory
974147nextgenxingThe Xana coup (BOI21_xanadu)C++14
100 / 100
49 ms25948 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ff first #define ss second #define all(x) begin(x), end(x) #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define F0R(i, x) FOR(i, 0, x) #define FORd(i, a, b) for(int i = (b)-1; i >= (a); i--) #define F0Rd(i, x) FORd(i, 0, x) #define ckif(a, b, c) ((c) ? (a) : (b)) const int MAX_N = 2e5+69; const ll INF = 0x3f3f3f3f; int n; vector<int> adj[MAX_N]; int dp[MAX_N][2][2], a[MAX_N]; // what it is, whether it has toggled void solve(int curr, int par = -1){ int dp2[2][2] = {0}; // what the it is, what all the children are dp2[a[curr]^1][0] = INF, dp2[a[curr]^1][1] = INF; for(auto x : adj[curr]){ if(x == par) continue; solve(x, curr); F0R(i1, 2) F0R(i2, 2) F0R(j1, 2) F0R(j2, 2) if(dp2[i1][i2] < INF && dp[x][j1][j2] < INF && i2 == j1) dp[curr][i1^j2][i2] = min(dp[curr][i1^j2][i2], dp2[i1][i2]+dp[x][j1][j2]); F0R(i, 2) F0R(j, 2) dp2[i][j] = dp[curr][i][j], dp[curr][i][j] = INF; } F0R(i, 2) F0R(j, 2) dp[curr][i^j][j] = dp2[i][j]+j; } int main(int argc, const char * argv[]){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cin >> n; F0R(i, n-1){ int a, b; cin >> a >> b; a--, b--; adj[a].push_back(b); adj[b].push_back(a); } F0R(i, n) cin >> a[i]; memset(dp, 0x3f, sizeof(dp)); solve(0); ll x = min(dp[0][0][0], dp[0][0][1]); if(x >= INF) cout << "impossible\n"; else cout << x << '\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...