제출 #668965

#제출 시각아이디문제언어결과실행 시간메모리
668965mychecksedadThe Xana coup (BOI21_xanadu)C++17
100 / 100
80 ms49588 KiB
/* Author : Mychecksdead */ #include<bits/stdc++.h> using namespace std; typedef long long int ll; #define pb push_back #define all(x) x.begin(), x.end() const int N = 1e6 + 5; ll n, dp[N][2][2], a[N]; vector<int> g[N]; void dfs(int v, int p){ vector<ll> no, yes; ll no_sum = 0, yes_sum = 0; for(int u: g[v]){ if(u == p) continue; dfs(u, v); no_sum += dp[u][0][0]; yes_sum += dp[u][0][1]; no.pb(dp[u][1][0] - dp[u][0][0]); yes.pb(dp[u][1][1] - dp[u][0][1]); } sort(all(no)); sort(all(yes)); int s = no.size(); dp[v][0][a[v]] = no_sum; dp[v][1][a[v]^1] = yes_sum + 1; for(int i = 0; i < s; ++i){ bool state = (i + 1) % 2; no_sum += no[i]; yes_sum += yes[i]; dp[v][0][a[v] ^ state] = min(dp[v][0][a[v] ^ state], no_sum); dp[v][1][a[v] ^ state ^ 1] = min(dp[v][1][a[v] ^ state ^ 1], yes_sum + 1); } } void solve(){ cin >> n; for(int i = 0; i < n - 1; ++i){ int u, v; cin >> u >> v; g[u].pb(v); g[v].pb(u); } for(int i = 1; i <= n; ++i) cin >> a[i]; for(int i = 1; i <= n; ++i) dp[i][0][0] = dp[i][0][1] = dp[i][1][0] = dp[i][1][1] = N; dfs(1, 0); if(min(dp[1][0][0], dp[1][1][0]) >= N){ cout << "impossible"; }else{ cout << min(dp[1][0][0], dp[1][1][0]); } } int main(){ cin.tie(0); ios::sync_with_stdio(0); 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...