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;
vector<vector<long long>>adj;
long long n;
const int maxn=100000+5;
long long dp[2][2][maxn],fake[2][2][maxn];
vector<long long>all;
long long inf=1e9+5;
void solve(long long u=1,long long par=0){
int cnt=0;
if(all[u]==1){
dp[0][1][u]=0;
dp[1][0][u]=1;
dp[0][0][u]=inf;
dp[1][1][u]=inf;
}
else{
dp[0][0][u]=0;
dp[1][1][u]=1;
dp[0][1][u]=inf;
dp[1][0][u]=inf;
}
for(auto x:adj[u]){
if(x!=par){
cnt++;
solve(x,u);
fake[0][0][u]=min(dp[0][0][u]+dp[0][0][x],dp[0][1][u]+dp[1][0][x]);
fake[0][1][u]=min(dp[0][1][u]+dp[0][0][x],dp[0][0][u]+dp[1][0][x]);
fake[1][0][u]=min(dp[1][0][u]+dp[0][1][x],dp[1][1][u]+dp[1][1][x]);
fake[1][1][u]=min(dp[1][1][u]+dp[0][1][x],dp[1][0][u]+dp[1][1][x]);
dp[0][0][u]=fake[0][0][u];
dp[1][0][u]=fake[1][0][u];
dp[0][1][u]=fake[0][1][u];
dp[1][1][u]=fake[1][1][u];
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
all.resize(n+1);
adj.resize(n+1);
for(long long i=1;i<=n-1;i++){
long long u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(long long i=1;i<=n;i++){
cin>>all[i];
}
solve(1);
long long res=min(dp[1][0][1],dp[0][0][1]);
if(res>=1e9+5){
cout<<"impossible"<<endl;
return 0;
}
cout<<res<<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... |