Submission #867091

#TimeUsernameProblemLanguageResultExecution timeMemory
867091HossamHero7The Xana coup (BOI21_xanadu)C++14
100 / 100
125 ms45220 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
vector<vector<int>> adj;
vector<int> on;
const int N = 1e5+5;
ll dp[N][3][2];
ll solve(int node,int par,int pressed,bool color){
    ll &ret = dp[node][pressed][color];
    if(~ret) return ret;
    ret = 1e9;
    if(pressed != 1){
        int sz = adj[node].size();
        if(node) sz --;
        vector<vector<ll>> dp2(2,vector<ll>(2));
        vector<pair<ll,ll>> ops;
        for(auto ch : adj[node]){
            if(ch == par) continue;
            ops.push_back({solve(ch,node,1,on[ch]^1) + 1 , solve(ch,node,0,on[ch])});
        }
        dp2[sz&1][color] = 0;
        dp2[sz&1][!color] = 1e9;
        for(int i=sz-1;i>=0;i--){
            for(int j=0;j<2;j++){
                dp2[i&1][j] = min(dp2[i+1&1][j]+ops[i].second , dp2[i+1&1][j^1] + ops[i].first);
            }
        }
        ret = min(ret,dp2[0][0]);
    }
    if(pressed != 0){
        int sz = adj[node].size();
        if(node) sz --;
        vector<vector<ll>> dp2(2,vector<ll>(2));
        vector<pair<ll,ll>> ops;
        for(auto ch : adj[node]){
            if(ch == par) continue;
            ops.push_back({solve(ch,node,1,on[ch]) + 1 , solve(ch,node,0,on[ch]^1)});
        }
        dp2[sz&1][color] = (pressed == 1 ? 0 : 1e9);
        dp2[sz&1][!color] = (pressed == 2 ? 0 : 1e9);
        for(int i=sz-1;i>=0;i--){
            for(int j=0;j<2;j++){
                dp2[i&1][j] = min(dp2[i+1&1][j]+ops[i].second , dp2[i+1&1][j^1] + ops[i].first);
            }
        }
        ret = min(ret,dp2[0][0] + (pressed == 2));
    }
    return ret;
}
void solve(){
    int n;
    cin>>n;
    adj.resize(n);
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        a -- , b --;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    on.resize(n);
    for(int i=0;i<n;i++){
        int x;cin>>x;
        if(x) on[i] = 1;
    }
    memset(dp,-1,sizeof(dp));
    ll ans = solve(0,n,2,on[0]);
    if(ans >= 1e9) cout<<"impossible"<<endl;
    else cout<<ans<<endl;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);      cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Compilation message (stderr)

xanadu.cpp: In function 'll solve(int, int, int, bool)':
xanadu.cpp:26:40: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   26 |                 dp2[i&1][j] = min(dp2[i+1&1][j]+ops[i].second , dp2[i+1&1][j^1] + ops[i].first);
      |                                       ~^~
xanadu.cpp:26:70: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   26 |                 dp2[i&1][j] = min(dp2[i+1&1][j]+ops[i].second , dp2[i+1&1][j^1] + ops[i].first);
      |                                                                     ~^~
xanadu.cpp:44:40: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   44 |                 dp2[i&1][j] = min(dp2[i+1&1][j]+ops[i].second , dp2[i+1&1][j^1] + ops[i].first);
      |                                       ~^~
xanadu.cpp:44:70: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   44 |                 dp2[i&1][j] = min(dp2[i+1&1][j]+ops[i].second , dp2[i+1&1][j^1] + ops[i].first);
      |                                                                     ~^~
#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...