Submission #879584

#TimeUsernameProblemLanguageResultExecution timeMemory
879584Shayan86The Xana coup (BOI21_xanadu)C++14
100 / 100
39 ms20572 KiB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

const ll N = 1e5 + 50;
const ll inf = 1e18;

ll n, col[N], dp[N][2][2], tmp[2][2];

vector<int> adj[N];

void dfs(int v, int p = 0){
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            dp[v][i][j] = inf;
        }
    }
    dp[v][col[v]][0] = 0;
    dp[v][1-col[v]][1] = 1;

    for(int u: adj[v]){
        if(u == p) continue;
        dfs(u, v);
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 2; j++){
                tmp[i][j] = inf;
            }
        }
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 2; j++){
                for(int k = 0; k < 2; k++){
                    tmp[i][j] = min(tmp[i][j], dp[u][j][k] + dp[v][i^k][j]);
                }
            }
        }
        for(int i = 0; i < 2; i++){
            for(int j = 0; j < 2; j++){
                dp[v][i][j] = tmp[i][j];
            }
        }
    }
}

int main(){
    fast_io;

    cin >> n;

    int u, v;
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }

    for(int i = 1; i <= n; i++) cin >> col[i];

    dfs(1);

    ll res = min(dp[1][0][0], dp[1][0][1]);

    if(res >= inf) kill("impossible");
    cout << res;

}
#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...