Submission #1105627

#TimeUsernameProblemLanguageResultExecution timeMemory
1105627koukirocksThe Xana coup (BOI21_xanadu)C++17
100 / 100
140 ms59612 KiB
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const ll MAX=1e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;

vvector<ll> dfs(ll v,ll p,vvector<ll> &G,vector<ll> &a) {
    vvector<ll> ans(2,vector<ll>(2));
    vvector<ll> rec(2,vector<ll>(2));
    rec[0][0]=0;
    rec[0][1]=INF;
    rec[1][0]=0;
    rec[1][1]=INF;
    for (ll i:G[v]) {
        if (i==p) continue;
        vvector<ll> now = dfs(i,v,G,a);
        vvector<ll> nrec(2,vector<ll>(2));
        nrec[0][0]=min(rec[0][1]+now[0][1],rec[0][0]+now[0][0]);
        nrec[0][1]=min(rec[0][1]+now[0][0],rec[0][0]+now[0][1]);
        nrec[1][0]=min(rec[1][1]+now[1][1],rec[1][0]+now[1][0]);
        nrec[1][1]=min(rec[1][1]+now[1][0],rec[1][0]+now[1][1]);
        rec=nrec;
    }
    if (a[v]) {
        ans[0][0] = rec[0][1];
        ans[0][1] = 1+rec[1][0];
        ans[1][0] = rec[0][0];
        ans[1][1] = 1+rec[1][1];
    } else {
        ans[0][0] = rec[0][0];
        ans[0][1] = 1+rec[1][1];
        ans[1][0] = rec[0][1];
        ans[1][1] = 1+rec[1][0];
    }
    // cout<<v<<" "<<rec[0][0]<<" "<<rec[0][1]<<" "<<rec[1][0]<<" "<<rec[1][1]<<" v rec\n";
    // cout<<v<<" "<<ans[0][0]<<" "<<ans[0][1]<<" "<<ans[1][0]<<" "<<ans[1][1]<<" v ans\n";
    return ans;
}
 
int main() {
    speed;
    ll n;
    cin>>n;
    vvector<ll> G(n+1);
    for (int i=0;i<n-1;i++) {
        ll a,b;
        cin>>a>>b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<ll> a(n+1);
    for (int i=1;i<=n;i++) {
        cin>>a[i];
    }
    vvector<ll> ans = dfs(1,0,G,a);
    ll fin = min(ans[0][0],ans[0][1]);
    if (fin>=INF) cout<<"impossible\n";
    else cout<<fin<<"\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...