이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
const int INF=1e9;
int n;
int arr[100005];
vector<int> al[100005];
int memo[100005][2][2];
int dfs(int i,int p,int on,int toggle){
if (memo[i][on][toggle]!=-1) return memo[i][on][toggle];
//if toggle=false, then our children must be on=false
//else children must be on=true
int val[2]={INF,INF};
val[arr[i]]=0;
for (auto &it:al[i]){
if (it==p) continue;
int tf=dfs(it,i,toggle,false);
int tt=dfs(it,i,toggle,true);
int temp[2]={min({val[0]+tf,val[1]+tt,INF}),
min({val[0]+tt,val[1]+tf,INF})};
swap(val,temp);
}
//cout<<i<<" "<<on<<" "<<toggle<<" "<<val[on]+toggle<<endl;
return memo[i][on][toggle]=val[on^toggle]+toggle;
}
int main(){
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(false);
cin>>n;
int a,b;
rep(x,1,n){
cin>>a>>b;
al[a].pub(b);
al[b].pub(a);
}
rep(x,1,n+1) cin>>arr[x];
memset(memo,-1,sizeof(memo));
int ans=min(dfs(1,-1,0,0),dfs(1,-1,0,1));
//cout<<dfs(1,-1,0,0)<<endl;
//cout<<dfs(1,-1,0,1)<<endl;
if (ans<1e9) cout<<ans<<endl;
else cout<<"impossible"<<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... |