Submission #466197

#TimeUsernameProblemLanguageResultExecution timeMemory
466197alirezasamimi100The Xana coup (BOI21_xanadu)C++17
100 / 100
131 ms29792 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
/*#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
using namespace std;
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define F first
#define S second
#define pb push_back
//#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=5e5+10,LN=20,M=5e6,SQ=350,B=737,inf=1e9+10;
const ll INF=1e18;
const ld ep=1e-7;
const int MH=1000696969,MD=998244353,MOD=1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
    ll ans=1;
    while (y != 0) {
        if (y & 1) ans = ans * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return ans;
}
ll n,dp[N][4],a[N],ans;
vector<ll> adj[N];
void dfs(ll v, ll p){
    for(ll u : adj[v]){
        if(u!=p) dfs(u,v);
    }
    for(ll i=0; i<4; i++){
        ll z=i&1,y=(i&2)/2,x=a[v]^z^y,mn=inf;
        if(y) dp[v][i]++;
        for(ll u : adj[v]){
            if(u==p) continue;
            if(dp[u][y]<=dp[u][y+2]){
                dp[v][i]+=dp[u][y];
                mn=min(mn,dp[u][y+2]-dp[u][y]);
            }else{
                dp[v][i]+=dp[u][y+2];
                mn=min(mn,dp[u][y]-dp[u][y+2]);
                x^=1;
            }
        }
        if(x) dp[v][i]+=mn;
    }
}
int main(){
    fast_io;
    cin >> n;
    for(ll i=1; i<n; i++){
        ll v,u;
        cin >> v >> u;
        adj[v].pb(u);
        adj[u].pb(v);
    }
    for(ll i=1; i<=n; i++) cin >> a[i];
    dfs(1,0);
    ans=min(dp[1][0],dp[1][2]);
    if(ans>n) cout << "impossible\n";
    else cout << ans << '\n';
    return 0;
}
#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...