Submission #1124126

#TimeUsernameProblemLanguageResultExecution timeMemory
1124126mmdrzadaThe Xana coup (BOI21_xanadu)C++20
100 / 100
50 ms24648 KiB
#include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef vector<char> vc; typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vll; #define sep ' ' #define F first #define S second #define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pb push_back #define kill(x) {cout << x << endl;return;} #define sz(x) int(x.size()) #define SP(x) setprecision(x) #define mod(x) (1ll*x%M+M)%M #define pq priority_queue #define mid (l+r)/2 // #define mid2 (l+r+1)/2 #define pll pair<ll, ll> #define REP(i, k) for (int i = 0 ; i < k ; i ++) #define MP make_pair #define us unordered_set const int INF = 1e9+10; const int N = 1e5+10; int n; vi adj[N]; bool s[N]; int dp[2][2][N]; void dfs(int v = 0, int p = -1) { // cout << v+1 << endl; if (adj[v].size() == 1 && adj[v][0] == p) { if (s[v]) { dp[1][0][v] = 1; dp[0][1][v] = 0; } else { dp[0][0][v] = 0; dp[1][1][v] = 1; } return; } bool flag0 = true, flag1 = true; for(int neigh: adj[v]) { if (neigh == p) continue; dfs(neigh, v); if (dp[0][0][neigh] == -1 && dp[1][0][neigh] == -1) { flag0 = false; } if (dp[0][1][neigh] == -1 && dp[1][1][neigh] == -1) { flag1 = false; } } if (flag0) { bool curr = s[v]; vector<int> vec; int cost = 0; for(int neigh: adj[v]) { if (neigh == p) continue; if (dp[0][0][neigh] == -1) { cost += dp[1][0][neigh]; curr = !curr; } else { cost += dp[0][0][neigh]; if (dp[1][0][neigh] != -1) { vec.pb(dp[1][0][neigh] - dp[0][0][neigh]); } } } dp[0][curr][v] = (dp[0][curr][v] == -1 ? cost : min(dp[0][curr][v], cost)); sort(vec.begin(), vec.end()); for(int delta: vec) { cost += delta; curr = !curr; dp[0][curr][v] = (dp[0][curr][v] == -1 ? cost : min(dp[0][curr][v], cost)); } } if (flag1) { bool curr = !s[v]; vector<int> vec; int cost = 1; // we changed the v itself ! for(int neigh: adj[v]) { if (neigh == p) continue; if (dp[0][1][neigh] == -1) { cost += dp[1][1][neigh]; curr = !curr; } else { cost += dp[0][1][neigh]; if (dp[1][1][neigh] != -1) { vec.pb(dp[1][1][neigh] - dp[0][1][neigh]); } } } dp[1][curr][v] = (dp[1][curr][v] == -1 ? cost : min(dp[1][curr][v], cost)); sort(vec.begin(), vec.end()); for(int delta: vec) { cost += delta; curr = !curr; dp[1][curr][v] = (dp[1][curr][v] == -1 ? cost : min(dp[1][curr][v], cost)); } } } void solve() { memset(dp, -1, sizeof dp); cin >> n; for(int i = 0 ; i < n-1 ; i ++) { int u, v; cin >> u >> v; u--, v--; adj[u].pb(v), adj[v].pb(u); } REP(i, n) cin >> s[i]; dfs(); int ans = n+1; if (dp[0][0][0] != -1) ans = min(ans, dp[0][0][0]); if (dp[1][0][0] != -1) ans = min(ans, dp[1][0][0]); if (ans <= n) cout << ans << endl; else cout << "impossible" << endl; } // check MAXN int32_t main() { fastIO; solve(); 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...