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>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define size(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
// DP[v][on/off][move];
ll DP[100001][2][2];
bool state[100001];
vector<int> graf[100001];
void dfs(int v, int pop)
{
forall(it,graf[v])
{
if(it != pop) dfs(it,v);
}
// 0 0
// cout << "xd\n";
vector<ll> moves;
ll sum = 0;
forall(it,graf[v])
{
if(it != pop)
{
sum += DP[it][0][0];
moves.pb(DP[it][0][1] - DP[it][0][0]);
}
}
sort(all(moves));
reverse(all(moves));
// forall(it,moves) cout << it << " ";
// cout << " moves\n";
//cout << sum << " s1\n";
if(state[v] == 1)
{
if(size(moves) == 0)
{
sum = 1e9;
// cout << "xd\n";
}
else
{
sum += moves[size(moves)-1];
moves.pop_back();
}
}
// cout << sum << " s2\n";
//cout << sum << " xd\n";
while(size(moves) > 1)
{
int end_ind = size(moves)-1;
if(moves[end_ind] + moves[end_ind-1] <= 0)
{
sum += moves[end_ind] + moves[end_ind-1];
moves.pop_back();
moves.pop_back();
}
else break;
}
DP[v][0][0] = sum;
// cout << "xd\n";
// 1 0
moves = {};
sum = 0;
forall(it,graf[v])
{
if(it != pop)
{
sum += DP[it][0][0];
// cout << DP[it][0][0] << " dp\n";
moves.pb(DP[it][0][1] - DP[it][0][0]);
}
}
//cout << sum << " sum\n";
sort(all(moves));
reverse(all(moves));
if(state[v] == 0)
{
if(size(moves) == 0)
{
sum = 1e9;
}
else
{
sum += moves[size(moves)-1];
moves.pop_back();
}
}
// cout << sum << " s\n";
while(size(moves) > 1)
{
int end_ind = size(moves)-1;
if(moves[end_ind] + moves[end_ind-1] <= 0)
{
sum += moves[end_ind] + moves[end_ind-1];
moves.pop_back();
moves.pop_back();
}
else break;
}
DP[v][1][0] = sum;
// cout << "xd\n";
// 0 1
moves = {};
sum = 1;
forall(it,graf[v])
{
if(it != pop)
{
sum += DP[it][1][0];
moves.pb(DP[it][1][1] - DP[it][1][0]);
}
}
// cout << "xd\n";
sort(all(moves));
reverse(all(moves));
if(state[v] == 0)
{
if(size(moves) == 0)
{
sum = 1e9;
}
else
{
sum += moves[size(moves)-1];
moves.pop_back();
}
}
//cout << "xd\n";
while(size(moves) > 1)
{
int end_ind = size(moves)-1;
if(moves[end_ind] + moves[end_ind-1] <= 0)
{
sum += moves[end_ind] + moves[end_ind-1];
moves.pop_back();
moves.pop_back();
}
else break;
}
DP[v][0][1] = sum;
// cout << "xd\n";
// 1 1
moves = {};
sum = 1;
forall(it,graf[v])
{
if(it != pop)
{
sum += DP[it][1][0];
moves.pb(DP[it][1][1] - DP[it][1][0]);
}
}
sort(all(moves));
reverse(all(moves));
if(state[v] == 1)
{
if(size(moves) == 0)
{
sum = 1e9;
}
else
{
sum += moves[size(moves)-1];
moves.pop_back();
}
}
while(size(moves) > 1)
{
int end_ind = size(moves)-1;
if(moves[end_ind] + moves[end_ind-1] <= 0)
{
sum += moves[end_ind] + moves[end_ind-1];
moves.pop_back();
moves.pop_back();
}
else break;
}
DP[v][1][1] = sum;
// cout << "xd\n";;;;
//cout << v << " " << DP[v][0][0] << " " << DP[v][0][1] << " " << DP[v][1][0] << " " << DP[v][1][1] << "dfs\n";
}
void solve()
{
int n;
cin >> n;
rep(i,n-1)
{
int a,b;
cin >> a >> b;
graf[a].pb(b);
graf[b].pb(a);
}
rep2(i,1,n) cin >> state[i];
dfs(1,1);
ll ans = min(DP[1][0][0],DP[1][0][1]);
if(ans >= 1e9) cout << "impossible";
else cout << ans;
}
int main()
{
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random();
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... |