This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* 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 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... |