Submission #1323860

#TimeUsernameProblemLanguageResultExecution timeMemory
1323860YSH2020The Xana coup (BOI21_xanadu)C++20
0 / 100
77 ms22172 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
vector<int> adj[100005];

int dp[100005][2];
int state[100005];
void solve(int n, int p) {
    for (auto i:adj[n]) {
        if (i!=p) solve(i,n);
    }
    for (int i = 0; i < 2; i++) {
        int newstate = state[n]^i;
        //by default, take all of that type first
        //you want to take newstate%2 such shit.
        vector<int> diff;
        int total = 0; for (auto x:adj[n]) if (x!=p) total += dp[x][0],diff.push_back(dp[x][1]-dp[x][0]);
        sort(diff.begin(), diff.end());
        int start=0;
        int ans=2e9;
        if (newstate==1 and diff.size()==0) {dp[n][i]=2e9;continue;}
        else if (diff.size()==0) {dp[n][i]=i; continue;}
        else if (newstate==1){
            total += diff[0];
            ans=total;
            start=1;
        }
        for (; start < diff.size()-1; start+=2) {
            total += diff[start];
            total += diff[start+1];
            ans=min(ans,total);
        }
        dp[n][i]=min(2000000000ll,ans+i);
    }
}


signed main() {
    int n; cin >> n;
    for (int i = 0; i < n-1; i++) {
        int a,b; cin >> a>> b;
        a--;b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (int i = 0; i < n; i++) cin >> state[i];
    solve(0,-1);
    int ans=min(dp[0][0], dp[0][1]);
    if (ans==2e9) cout << "impossible";
    else cout << ans;
}
#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...