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