# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
897413 | LCJLY | The Xana coup (BOI21_xanadu) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
vector<int>adj[100005];
int arr[100005];
int dp[2][2][100005];
void dfs(int index, int par){
array<int,2>cnt;
array<int,2>take;
array<int,2>change;
cnt={0,0};
take={0,0};
change={(int)1e15,(int)1e15};
for(auto it:adj[index]){
if(it==par) continue;
dfs(it,index);
for(int x=0;x<2;x++){
if(dp[0][x][it]<dp[1][x][it]){
//no toggle is better
take[x]+=dp[0][x][it];
change[x]=min(change[x],dp[1][x][it]-dp[0][x][it]);
//change from no toggle to toggle
}
else{
//toggle is better
take[x]+=dp[1][x][it];
cnt[x]++;
change[x]=min(change[x],dp[0][x][it]-dp[1][x][it]);
//change from toggle to no toggle
}
}
}
int bit=arr[index];
//to achieve 0 0 (no toggle, and value is 0) need to take all 0 from child, but if toggle odd number of times, then fix back
//by toggling one less child or one more child
dp[0][0][index]=take[0]+((cnt[0]+bit)%2?change[0]:0);
//to achieve 0 1 (no toggle and value is 1) need take all 0 from child, but if toggle even number of times then fix back
dp[0][1][index]=take[0]+(!((cnt[0]+bit)%2)?change[0]:0);
dp[1][0][index]=1+take[1]+(!((cnt[1]+bit)%2)?change[1]:0);
dp[1][1][index]=1+take[1]+((cnt[1]+bit)%2?change[1]:0);
}
void solve(){
int n;
cin >> n;
int temp,temp2;
for(int x=0;x<n-1;x++){
cin >> temp >> temp2;
adj[temp].push_back(temp2);
adj[temp2].push_back(temp);
}
for(int x=1;x<=n;x++){
cin >> arr[x];
}
dfs(1,-1);
int best=min({dp[0][0][1],dp[1][0][1]); //force this node to be 0, invert works when normal works
if(best>=1e12) cout << "impossible\n";
else cout << best;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("tribool.in", "r", stdin);
int t=1;
//cin >> t;
while(t--){
solve();
}
}