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