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 f first
#define s second
#define pb push_back
using namespace std;
ll n,c[100005];
vector<ll>v[100005];
ll dp[100005][2][2];
void solve(ll x,ll par){
ll keon = 1e9,luon = 0,keof = 1e9,luof = 0;
bool ye = 0;
for(auto to : v[x]){
if(to == par)continue;
solve(to , x);
ye = 1;
ll wk = keon,wl = luon;
keon = min(wk + dp[to][0][1] , wl + dp[to][1][1]);
luon = min(wl + dp[to][0][1] , wk + dp[to][1][1]);
wk = keof , wl = luof;
keof = min(wk + dp[to][0][0] , wl + dp[to][1][0]);
luof = min(wl + dp[to][0][0] , wk + dp[to][1][0]);
}
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
dp[x][i][j] = 1e9;
if(!ye){
dp[x][0][c[x]] = 0;
dp[x][1][c[x] ^ 1] = 1;
return;
}
dp[x][0][1 ^ c[x]] = min(dp[x][0][1 ^ c[x]] , keof);
dp[x][0][c[x]] = min(dp[x][0][c[x]] , luof);
dp[x][1][c[x]] = min(dp[x][1][c[x]] , keon + 1);
dp[x][1][c[x] ^ 1] = min(dp[x][1][c[x] ^ 1] , luon + 1);
}
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<n; i++){
ll x,y;
cin >> x >> y;
v[x].pb(y);
v[y].pb(x);
}
for(int i=1; i<=n; i++){
cin >> c[i];
}
solve(1 , 0);
ll ans = min(dp[1][1][0] , dp[1][0][0]);
if(ans > 1e8)
cout << "impossible\n";
else
cout << ans << '\n';
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... |