# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
414791 | juggernaut | The Xana coup (BOI21_xanadu) | C++17 | 129 ms | 29384 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
void usaco(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
typedef long long ll;
#define USING_ORDERED_SET 0
#if USING_ORDERED_SET
#include<bits/extc++.h>
using namespace __gnu_pbds;
template<class T>using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#endif
template<class T>void umax(T &a,T b){if(a<b)a=b;}
template<class T>void umin(T &a,T b){if(b<a)a=b;}
#ifdef IOI2021SG
#define printl(args...)printf(args)
#else
#define printl(args...)((void)0)
#endif
int a[100005],n;
vector<int>g[100005];
int dp[100005][2][2];
struct knapsack{
int odd,even;
int sum;
knapsack(){
sum=0;
odd=even=2e5;
}
vector<int>v;
void add(int without,int with){
v.push_back(with-without);
sum+=without;
}
void build(){
sort(v.begin(),v.end());
umin(even,sum);
int pref=sum;
for(int i=0;i<v.size();i++){
pref+=v[i];
if(i&1){
umin(even,pref);
}else{
umin(odd,pref);
}
}
}
};
void dfs(int v,int p){
dp[v][0][0]=2e5;
dp[v][0][1]=2e5;
dp[v][1][0]=2e5;
dp[v][1][1]=2e5;
for(int to:g[v])if(to!=p)dfs(to,v);
int sz=g[v].size();
if(v^p)sz--;
if(sz==0){
if(a[v]==0){
dp[v][0][0]=0;
dp[v][0][1];
dp[v][1][0];
dp[v][1][1]=1;
}else{
dp[v][0][0];
dp[v][0][1]=0;
dp[v][1][0]=1;
dp[v][1][1];
}
}else{
knapsack a,b;
for(int to:g[v])if(to!=p){
a.add(dp[to][0][0],dp[to][1][0]);
b.add(dp[to][0][1],dp[to][1][1]);
}
a.build();
b.build();
if(::a[v]==0){
dp[v][0][0]=a.even;
dp[v][0][1]=a.odd;
dp[v][1][0]=b.odd+1;
dp[v][1][1]=b.even+1;
}else{
dp[v][0][0]=a.odd;
dp[v][0][1]=a.even;
dp[v][1][0]=b.even+1;
dp[v][1][1]=b.odd+1;
}
}
/*
if(sz==0){
if(a[v]==0){
dp[v][0][0]=0;
dp[v][0][1];
dp[v][1][0];
dp[v][1][1]=1;
}else{
dp[v][0][0];
dp[v][0][1]=0;
dp[v][1][0]=1;
dp[v][1][1];
}
}else if(sz==1){
if(a[v]==0){
dp[v][0][0]=dp[x][0][0];
dp[v][0][1]=dp[x][1][0];
dp[v][1][0]=dp[x][1][1]+1;
dp[v][1][1]=dp[x][0][1]+1;
}else{
dp[v][0][0]=dp[x][1][0];
dp[v][0][1]=dp[x][0][0];
dp[v][1][0]=dp[x][0][1]+1;
dp[v][1][1]=dp[x][1][1]+1;
}
}else if(sz==2){
if(a[v]==0){
dp[v][0][0]=min(dp[x][0][0]+dp[y][0][0],dp[x][1][0]+dp[y][1][0]);
dp[v][0][1]=min(dp[x][1][0]+dp[y][0][0],dp[x][0][0]+dp[y][1][0]);
dp[v][1][0]=min(dp[x][1][1]+dp[y][0][1],dp[x][0][1]+dp[y][1][1])+1;
dp[v][1][1]=min(dp[x][0][1]+dp[y][0][1],dp[x][1][1]+dp[y][1][1])+1;
}else{
dp[v][0][0]=min(dp[x][1][0]+dp[y][0][0],dp[x][0][0]+dp[y][1][0]);
dp[v][0][1]=min(dp[x][0][0]+dp[y][0][0],dp[x][1][0]+dp[y][1][0]);
dp[v][1][0]=min(dp[x][0][1]+dp[y][0][1],dp[x][1][1]+dp[y][1][1])+1;
dp[v][1][1]=min(dp[x][1][1]+dp[y][0][1],dp[x][0][1]+dp[y][1][1])+1;
}
}
*/
}
int main(){
scanf("%d",&n);
for(int i=1,x,y;i<n;i++){
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
dfs(1,1);
int x=min(dp[1][1][0],dp[1][0][0]);
if(x>=int(2e5))puts("impossible");
else printf("%d",x);
}
Compilation message (stderr)
# | 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... |