Submission #429819

#TimeUsernameProblemLanguageResultExecution timeMemory
429819Ronin13The Xana coup (BOI21_xanadu)C++14
0 / 100
151 ms20592 KiB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ull unsigned ll
#define epb emplace_back
#define pb push_back
#define inf 1e9+1
#define linf 1e18+1;
using namespace std;

ll dp[100001][2][2];
ll dp1[100001][2][2];
ll d[100001];
vector<vector<int> >g(100001);
int c[100001];
void dfs(int v,int e=-1){
    bool ind=false;
    if(e!=-1)
    dp[v][c[v]][c[e]]=dp1[v][c[v]][c[e]]=0;

    for(int to:g[v]){
        if(to==e)continue;
        ind=true;
        dfs(to,v);
        dp1[v][0][1]=min({dp[to][0][0]+dp[v][0][1],dp[to][1][1]+dp[v][1][0]+1,(ll)inf});
        dp1[v][1][0]=min({dp[to][0][1]+dp[v][1][0],dp[to][1][0]+dp[v][0][1]+1,(ll)inf});
        dp1[v][0][0]=min({dp[to][0][0]+dp[v][0][0],dp[to][1][1]+dp[v][1][1]+1,(ll)inf});
        dp1[v][1][1]=min({dp[to][0][1]+dp[v][1][1],dp[to][1][0]+dp[v][0][0]+1,(ll)inf});

        dp[v][0][1]=dp1[v][0][1];
        dp[v][1][0]=dp1[v][1][0];
        dp[v][0][0]=dp1[v][0][0];
        dp[v][1][1]=dp1[v][1][1];


    }
    if(ind==false){
        dp1[v][0][1]=min(dp[v][0][1],dp[v][1][0]+1);
        dp1[v][0][0]=min(dp[v][0][0],dp[v][1][1]+1);
        dp1[v][1][0]=min(dp[v][1][0],dp[v][0][1]+1);
        dp1[v][1][1]=min(dp[v][1][1],dp[v][0][0]+1);
        dp[v][0][1]=dp1[v][0][1];
        dp[v][1][0]=dp1[v][1][0];
        dp[v][0][0]=dp1[v][0][0];
        dp[v][1][1]=dp1[v][1][1];
    }

}



void solve(){
    int n;cin>>n;
    for(int i=2;i<=n;i++){
        dp[i][0][0]=dp[i][1][1]=dp[i][0][1]=dp[i][1][0]=inf;
         dp1[i][0][0]=dp1[i][1][1]=dp1[i][0][1]=dp1[i][1][0]=inf;
    }
    for(int i=1;i<=n-1;i++){
        int u,v;cin>>u>>v;
        g[u].pb(v),g[v].pb(u);
    }
    for(int i=1;i<=n;i++)cin>>c[i];

    dfs(1);
    if(dp[1][0][0]>=inf)cout<<"impossible";
    else cout<<dp[1][0][0];
}

int main(){
    solve();
}
#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...