Submission #414792

#TimeUsernameProblemLanguageResultExecution timeMemory
414792juggernautThe Xana coup (BOI21_xanadu)C++17
100 / 100
120 ms31180 KiB
#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];
ll dp[100005][2][2];
struct knapsack{
    ll odd,even;
    ll 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);
        ll 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)

xanadu.cpp: In member function 'void knapsack::build()':
xanadu.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for(int i=0;i<v.size();i++){
      |                     ~^~~~~~~~~
xanadu.cpp: In function 'void dfs(int, int)':
xanadu.cpp:60:23: warning: statement has no effect [-Wunused-value]
   60 |             dp[v][0][1];
      |             ~~~~~~~~~~^
xanadu.cpp:61:23: warning: statement has no effect [-Wunused-value]
   61 |             dp[v][1][0];
      |             ~~~~~~~~~~^
xanadu.cpp:64:23: warning: statement has no effect [-Wunused-value]
   64 |             dp[v][0][0];
      |             ~~~~~~~~~~^
xanadu.cpp:67:23: warning: statement has no effect [-Wunused-value]
   67 |             dp[v][1][1];
      |             ~~~~~~~~~~^
xanadu.cpp: In function 'void usaco(std::string)':
xanadu.cpp:5:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | void usaco(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xanadu.cpp:5:66: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | void usaco(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
      |                                                           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
xanadu.cpp: In function 'int main()':
xanadu.cpp:130:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
xanadu.cpp:132:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         scanf("%d%d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~
xanadu.cpp:136:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |     for(int i=1;i<=n;i++)scanf("%d",&a[i]);
      |                          ~~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...