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;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void setIO()
{
fast;
}
int n = 1e5 + 5;
vvi adj(n);
vvvl dp(n,vvl(2,vl(2,1e9)));
vl vals(n);
void dfs(int u,int p)
{
dp[u][vals[u]][0] = dp[u][vals[u]][1] = 0;
for (int v:adj[u]){
if (v == p)continue;
dfs(v,u);
vvl temp(4,vl(4));
for (int f1 = 0;f1<2;f1++){
for (int f2 = 0;f2<2;f2++){
temp[f1][f2] = min(
dp[u][f1][f2] + dp[v][f2][0],
dp[u][!f1][f2] + dp[v][!f2][1] + 1
);
}
}
for (int f1 = 0;f1<2;f1++)for (int f2 = 0;f2<2;f2++)dp[u][f1][f2] = temp[f1][f2];
}
}
int main()
{
setIO();
cin>>n;
for (int i = 1;i<=n - 1;i++){
int u,v;
cin>>u>>v;
adj[u].pb(v),adj[v].pb(u);
}
for (int i = 1;i<=n;i++)cin>>vals[i];
dfs(1,0);
ll ans = min(dp[1][0][0],dp[1][1][1] + 1);
if (ans > n)cout<<"impossible";
else cout<<ans;
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... |