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 : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
const int N = 1e5 + 7;
const int inf = 1e9 + 7;
int n , dp[N][4] , value[N];
vector < int > tree[N];
bool is_leaf(int node){
if(node == 1 and sz(tree[node]) == 0)return 1;
else if(sz(tree[node]) == 1)return 1;
else return 0;
}
void dfs(int node , int par){
for(auto itr : tree[node]){
if(itr != par){
dfs(itr , node);
}
}
dp[node][0] = dp[node][1] = dp[node][2] = dp[node][3] = inf;
// dp[node][0]
{
vector < int > v;
int sum = 0;
for(auto itr : tree[node]){
if(itr != par){
v.push_back(dp[itr][3] - dp[itr][0]);
sum += dp[itr][0];
}
}
sort(all(v));
v.insert(v.begin() , 0);
for(int x = value[node];x < sz(v);x+=2){
if(x > 0)sum += v[x-1];
sum += v[x];
dp[node][0] = min(dp[node][0] , sum + x);
}
}
// dp[node][1]
{
vector < int > v;
int sum = 0;
for(auto itr : tree[node]){
if(itr != par){
v.push_back(dp[itr][3] - dp[itr][0]);
sum += dp[itr][0];
}
}
sort(all(v));
v.insert(v.begin() , 0);
for(int x = 1 - value[node];x < sz(v);x+=2){
if(x > 0)sum += v[x-1];
sum += v[x];
dp[node][1] = min(dp[node][1] , sum + x);
}
}
// dp[node][2]
{
vector < int > v;
int sum = 0;
for(auto itr : tree[node]){
if(itr != par){
v.push_back(dp[itr][2] - dp[itr][1]);
sum += dp[itr][1];
}
}
sort(all(v));
v.insert(v.begin() , 0);
for(int x = value[node];x < sz(v);x+=2){
if(x > 0)sum += v[x-1];
sum += v[x];
dp[node][2] = min(dp[node][2] , sum + x);
}
}
// dp[node][3]
{
vector < int > v;
int sum = 0;
for(auto itr : tree[node]){
if(itr != par){
v.push_back(dp[itr][2] - dp[itr][1]);
sum += dp[itr][1];
}
}
sort(all(v));
v.insert(v.begin() , 0);
for(int x = 1 - value[node];x < sz(v);x+=2){
if(x > 0)sum += v[x-1];
sum += v[x];
dp[node][3] = min(dp[node][3] , sum + x);
}
}
if(sz(tree[node]) == 1 and node != 1){
dp[node][0] = min(dp[node][0] , dp[node][2]);
dp[node][2] = dp[node][0];
dp[node][1] = min(dp[node][1] , dp[node][3]);
dp[node][3] = dp[node][1];
}
// cout << node << " : " << dp[node][0] << " " << dp[node][1] << " " << dp[node][2] << " " << dp[node][3] << endl;
}
void solve(){
cin >> n;
for(int i = 1;i<n;i++){
int a,b;
cin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 1;i<=n;i++){
cin >> value[i];
}
dfs(1,0);
int ans = min(dp[1][0] , dp[1][3] + 1);
if(ans >= inf)cout << "impossible" << '\n';
else cout << ans << '\n';
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
# | 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... |