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 all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=1e5+9;
int f[maxn][2][2];
vector<int>a[maxn];
int b[maxn];
int n;
void dfs(int u,int par){
//f[i][j][k] j use i switch or not ?
for1(i,0,1){
for1(j,0,1){
f[u][i][j]=n+1;
}
}
f[u][0][b[u]]=0;
f[u][1][1^b[u]]=1;
for(auto v:a[u]){
if (v==par)continue;
dfs(v,u);
//if use
int v1=f[u][1][1],v0=f[u][1][0];
f[u][1][0]=min(v1+f[v][1][1],v0+f[v][0][1]);
f[u][1][1]=min(v1+f[v][0][1],v0+f[v][1][1]);
//if not use
v1=f[u][0][1],v0=f[u][0][0];
f[u][0][0]=min(v1+f[v][1][0],v0+f[v][0][0]);
f[u][0][1]=min(v1+f[v][0][0],v0+f[v][1][0]);
//cout<<v<<" "<<f[u][0][1]<<" "<<f[u][0][0]<<'\n';
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n;
for1(i,1,n-1){
int u,v;
cin>>u>>v;
a[u].pb(v);
a[v].pb(u);
}
for1(i,1,n)cin>>b[i];
dfs(1,0);
int ans=min(f[1][1][0],f[1][0][0]);
if (ans>=n+1)cout<<"impossible";
else cout<<ans;
}
# | 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... |