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 ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << endl
#define all(x) x.begin(),x.end()
vector<int> adj[100010];
ll dp[100010][2][2];
bool a[100010];
void dfs(int u,int p)
{
if(a[u])
{
dp[u][0][0]=1e9;
dp[u][0][1]=1;
dp[u][1][0]=0;
dp[u][1][1]=1e9;
}
else
{
dp[u][0][0]=0;
dp[u][0][1]=1e9;
dp[u][1][0]=1e9;
dp[u][1][1]=1;
}
for(auto i : adj[u])
{
if(i!=p)
{
dfs(i,u);
ll x[4]={dp[u][0][0],dp[u][0][1],dp[u][1][0],dp[u][1][1]};
dp[u][0][0]=min(dp[i][0][0]+x[0],dp[i][0][1]+x[2]);
dp[u][0][1]=min(dp[i][1][0]+x[1],dp[i][1][1]+x[3]);
dp[u][1][0]=min(dp[i][0][0]+x[2],dp[i][0][1]+x[0]);
dp[u][1][1]=min(dp[i][1][0]+x[3],dp[i][1][1]+x[1]);
}
}
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;cin>>n;
for(int i=1;i<n;i++)
{
int u,v;cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
for(int i=1;i<=n;i++) cin>>a[i];
dfs(1,-1);
ll ans=min(dp[1][0][0],dp[1][0][1]);
if(ans>=1e9) 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... |