This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int MAXN = 1e5+10, MOD = 1e9+7;
const int INF = 1e9+10;
int n, a[MAXN];
vector<int> adj[MAXN];
ar<ar<int, 2>, 2> dfs(int c, int p) { //par flipped[me flipped]
vector<ar<ar<int, 2>, 2>> sub;
for (auto nxt : adj[c]) if (nxt != p) sub.push_back(dfs(nxt, c));
ar<ar<int, 2>, 2> ans;
for (int rep : {0, 1}) {
for (int me : {0, 1}) {
ans[rep][me] = INF;
int base = 0;
vector<int> v;
for (auto x : sub) {
base += x[me][0];
v.push_back(x[me][1]-x[me][0]);
}
sort(v.begin(), v.end());
for (int i = 1; i < sz(v); i++) v[i] += v[i-1];
for (int cnt = a[c]; cnt <= sz(v); cnt += 2) {
int cost = base + me;
if (cnt) cost += v[cnt-1];
ans[rep][me] = min(ans[rep][me], cost);
}
a[c] ^= 1;
}
a[c] ^= 1;
}
return ans;
}
void solve() {
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b; cin >> a >> b, --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
for (int i = 0; i < n; i++) cin >> a[i];
auto dp = dfs(0, -1);
int ans = min(dp[0][0], dp[0][1]);
if (ans >= INF) cout << "impossible\n";
else cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int T=1;
//cin >> T;
while (T--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |