제출 #1288999

#제출 시각아이디문제언어결과실행 시간메모리
1288999djawadmainThe Xana coup (BOI21_xanadu)C++20
30 / 100
37 ms16960 KiB
#include<iostream>
#include<vector>

using namespace std;

#define ll long long
#define ld long double
#define pii pair<int, int>
#define RUN_LIKE_DJAWAD (ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0));
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define maxn 100001

vector<int> g[maxn];

int dp[maxn][2][2];

int n, ui, vi;

bool s[maxn], seen[maxn];

void dfs(int v){

    seen[v] = true;

    int a0 = 0, d0 = 1e9, a1 = 0, d1 = 1e9;

    bool c0 = 0, c1 = 0;

    for (int u: g[v]) if (not seen[u]){

        dfs(u);

        a0 += min(dp[u][0][0], dp[u][0][1]);
        d0 = min(d0, abs(dp[u][0][0] - dp[u][0][1]));

        if (dp[u][0][1] < dp[u][0][0]) c0 ^= 1;

        a1 += min(dp[u][1][0], dp[u][1][1]);
        d1 = min(d1, abs(dp[u][1][0] - dp[u][1][1]));

        if (dp[u][1][1] < dp[u][1][0]) c1 ^= 1;
    }

    if (c0){

        a0 += d0;
        d0 *= -1;
    }
    if (c1){

        a1 += d1;
        d1 *= -1;
    }

    if (s[v]){

        dp[v][0][0] = a0 + d0;
        dp[v][0][1] = a1 + 1;
        dp[v][1][0] = a0;
        dp[v][1][1] = a1 + d1 + 1;

    } else {

        dp[v][0][0] = a0;
        dp[v][0][1] = a1 + d1 + 1;
        dp[v][1][0] = a0 + d0;
        dp[v][1][1] = a1 + 1;
    }
}

int main(){

    RUN_LIKE_DJAWAD

    cin >> n;

    for (int i = 1; i < n; i++){

        cin >> ui >> vi;

        g[ui].pb(vi);
        g[vi].pb(ui);
    }

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

    dfs(1);

    int ans = min(dp[1][0][0], dp[1][0][1]);

    if (ans >= 1e9){

        cout << "impossible\n";
        return 0;
    }

    cout << ans << "\n";
}
#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...