Submission #1184807

#TimeUsernameProblemLanguageResultExecution timeMemory
1184807user192837Power Plant (JOI20_power)C++17
100 / 100
90 ms29508 KiB
#include <bits/stdc++.h>
#define ar array
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;

const int N = 2e5 + 5;
vector <int> g[N];
int a[N], dp[N], n, ans = 0;

void dfs(int x, int par) {
    int sm = 0;
    for (int y : g[x]) {
        if (y != par) {
            dfs(y, x);
            ans = max(ans, dp[y] + a[x]);
            sm += dp[y];
        }
    }
    dp[x] = max(sm - a[x], dp[x]);
    ans = max(ans, dp[x]);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    for (int i = 1; i <= n; i++) {
        char c;
        cin >> c;
        a[i] = dp[i] = c - '0';
    }
    dfs(1, 0);
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...