Submission #1308381

#TimeUsernameProblemLanguageResultExecution timeMemory
1308381Born_To_LaughThe Xana coup (BOI21_xanadu)C++17
30 / 100
35 ms17060 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 1e9 + 7;

const int maxn = 1e5 + 10;
int n;
int cc[maxn];
vector<int> adj[maxn];
ll dp[maxn][2][2];

void dfs(int a, int par){
    if(cc[a]){
        dp[a][1][0] = 1;
        dp[a][0][1] = 0;
        dp[a][1][1] = INF;
        dp[a][0][0] = INF;
    }
    else{
        dp[a][1][0] = INF;
        dp[a][0][1] = INF;
        dp[a][1][1] = 1;
        dp[a][0][0] = 0;
    }
    for(auto &elm: adj[a]){
        if(elm == par)continue;
        dfs(elm, a);
        int nxt[2][2];
        for(int i=0; i<2; ++i){
            for(int j=0; j<2; ++j){
                nxt[i][j] = min({
                    dp[a][i][0] + dp[elm][j][i],
                    dp[a][i][1] + dp[elm][1 ^ j][i]
                });
            }
        }
        for(int i=0; i<2; ++i){
            for(int j=0; j<2; ++j){
                dp[a][i][j] = nxt[i][j];
            }
        }
    }
}
void solve(){
    cin >> n;
    for(int i=1; i<n; ++i){
        int a, b;cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for(int i=1; i<=n; ++i)cin >> cc[i];
    dfs(1, -1);


    int ans = min(dp[1][1][0], dp[1][0][0]);
    if(ans > maxn){
        cout << "impossible" << '\n';
    }
    else cout << ans << '\n';
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    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...