이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |