Submission #415122

# Submission time Handle Problem Language Result Execution time Memory
415122 2021-05-31T14:46:03 Z achibasadzishvili The Xana coup (BOI21_xanadu) C++17
0 / 100
73 ms 19000 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll n,c[100005];
vector<ll>v[100005];
ll dp[100005][2][2];
void solve(ll x,ll par){
    ll keon = 1e9,luon = 0,keof = 1e9,luof = 0;
    bool ye = 0;
    for(auto to : v[x]){
        if(to == par)continue;
        solve(to , x);
        ye = 1;
        ll wk = keon,wl = luon;
        keon = min(wk + dp[to][0][1] , wl + dp[to][1][1]);
        luon = min(wl + dp[to][0][1] , wk + dp[to][1][1]);
        wk = keof , wl = luof;
        keof = min(wk + dp[to][0][0] , wl + dp[to][1][0]);
        luof = min(wl + dp[to][0][0] , wk + dp[to][1][0]);
    }
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            dp[x][i][j] = 1e9;
    if(!ye){
        dp[x][0][c[x]] = 0;
        dp[x][1][c[x] ^ 1] = 1;
        return;
    }
    dp[x][0][1 ^ c[x]] = min(dp[x][0][1 ^ c[x]] , keof);
    dp[x][0][c[x]] = min(dp[x][0][c[x]] , luof);
    dp[x][1][c[x]] = min(dp[x][1][c[x]] , keon + 1);
    dp[x][1][c[x] ^ 1] = min(dp[x][1][c[x]] , luon + 1);
    /*cout << x << '\n';
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            cout << i << " " << j << " " << dp[x][i][j] << '\n';
    cout << "Done\n";
    */
}
int main(){
    ios::sync_with_stdio(false);
    cin >> n;
    for(int i=1; i<n; i++){
        ll x,y;
        cin >> x >> y;
        v[x].pb(y);
        v[y].pb(x);
    }
    
    for(int i=1; i<=n; i++){
        cin >> c[i];
    }
    
    solve(1 , 0);
    ll ans = min(dp[1][1][0] , dp[1][0][0]);
    if(ans > 1e8){
        while(true);
    }
    else
        cout << ans << '\n';
    
    
    
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Incorrect 2 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Incorrect 2 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 73 ms 18892 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 69 ms 19000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Incorrect 2 ms 2636 KB Output isn't correct
3 Halted 0 ms 0 KB -