Submission #991366

#TimeUsernameProblemLanguageResultExecution timeMemory
99136612345678The Xana coup (BOI21_xanadu)C++17
100 / 100
45 ms18616 KiB
#include <bits/stdc++.h>

using namespace std;

const int nx=1e5+5, inf=1e9;

int n, a[nx], u, v, dp[nx][2][2], tmp[2][2];
vector<int> d[nx];

void dfs(int u, int p)
{
    int dp2[2][2];
    dp2[0][0]=dp2[1][0]=0;
    dp2[0][1]=dp2[1][1]=inf;
    for (auto v:d[u])
    {
        if (v==p) continue;
        dfs(v, u);
        tmp[0][0]=min(dp2[0][0]+dp[v][0][0], dp2[0][1]+dp[v][0][1]);
        tmp[0][1]=min(dp2[0][1]+dp[v][0][0], dp2[0][0]+dp[v][0][1]);
        tmp[1][0]=min(dp2[1][0]+dp[v][1][0], dp2[1][1]+dp[v][1][1]);
        tmp[1][1]=min(dp2[1][1]+dp[v][1][0], dp2[1][0]+dp[v][1][1]);
        for (int i=0; i<2; i++) for (int j=0; j<2; j++) dp2[i][j]=min(inf, tmp[i][j]);
        //cout<<"dp "<<v<<' '<<u<<' '<<dp2[0][0]<<' '<<dp2[0][1]<<' '<<dp2[1][0]<<' '<<dp2[1][1]<<'\n';
    }
    //cout<<"dp "<<u<<' '<<dp2[0][0]<<' '<<dp2[0][1]<<' '<<dp2[1][0]<<' '<<dp2[1][1]<<'\n';
    if (a[u])
    {
        dp[u][0][0]=min(inf, dp2[1][0]);
        dp[u][0][1]=min(inf, dp2[0][1]+1);
        dp[u][1][0]=min(inf, dp2[1][1]);
        dp[u][1][1]=min(inf, dp2[0][0]+1);
    }
    else
    {
        dp[u][0][0]=min(inf, dp2[1][1]);
        dp[u][0][1]=min(inf, dp2[0][0]+1);
        dp[u][1][0]=min(inf, dp2[1][0]);
        dp[u][1][1]=min(inf, dp2[0][1]+1);
    }
    //cout<<"debug "<<u<<' '<<dp[u][0][0]<<' '<<dp[u][0][1]<<' '<<dp[u][1][0]<<' '<<dp[u][1][1]<<'\n';
}

/*
dp2[0][0]=all white, even press
dp2[0][1]=all white, odd press
dp2[1][0]=all black, even press
dp2[1][1]=all black, odd press

dp[0][0]=all white, no press
dp[0][1]=all white, press
dp[1][0]=black node, no press
dp[1][1]=black node, press
*/

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    for (int i=1; i<=n; i++) cin>>a[i];
    dfs(1, 1);
    if (min(dp[1][1][0], dp[1][1][1])>n) cout<<"impossible";
    else cout<<min(dp[1][1][0], dp[1][1][1]);
}
#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...