Submission #1033530

#TimeUsernameProblemLanguageResultExecution timeMemory
1033530adaawfPower Plant (JOI20_power)C++17
47 / 100
45 ms14672 KiB
#include <iostream>
#include <vector>
using namespace std;
int f[200005][2], a[200005];
vector<int> g[100005];
void dfs(int x, int p) {
    if (p != -1 && g[x].size() == 1) {
        f[x][0] = f[x][1] = a[x];
        return;
    }
    int c = 0, d = g[x].size() - (p != -1);
    if (d > 1) d = 1;
    else d = 0;
    for (int w : g[x]) {
        if (w == p) continue;
        dfs(w, x);
        c += f[w][1];
        f[x][0] = max(f[x][0], f[w][0]);
        f[x][0] = max(f[x][0], f[w][1] + a[x]);
    }
    f[x][1] = max(a[x], c - a[x]);
    f[x][0] = max(f[x][0], c - a[x] * d);
    //cout << x << " " << f[x][1] << " " << f[x][0] << '\n';
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    string s;
    cin >> s;
    s = " " + s;
    for (int i = 1; i <= n; i++) a[i] = (s[i] == '1');
    dfs(1, -1);
    cout << max(f[1][0], f[1][1]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...