CF1324F Maximum White Subtree solution to a problem tree DP

Topic links: https://www.luogu.com.cn/problem/CF1324F

Outline of Solution:
sides dfs, seeking the first pass \ ([i] f1 \) represented by \ (I \) as a root node and comprising \ (I \) maxima; seeking second pass \ (f2 [i] \) represents \ (I \) to take the parent node (and does not include \ (I \) of the maximum value) ( \ (F2 [I] \ GE 0 \) , if the \ (f2 [i] == 0 \) can be understood as \ (i \) those are not the top).

Codes are as follows:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 200020;
int n, a[maxn], f1[maxn], pa[maxn], f2[maxn];
vector<int> g[maxn];
void dfs1(int u) {
    f1[u] = a[u];
    int sz = g[u].size();
    for (int i = 0; i < sz; i ++) {
        int v = g[u][i];
        if (v == pa[u]) continue;
        pa[v] = u;
        dfs1(v);
        if (f1[v] > 0) f1[u] += f1[v];
    }
}
void dfs2(int u) {
    f2[u] = 0;
    if (pa[u]) {
        int pp = pa[u];
        if (f1[u] > 0) f2[u] = f1[pp] + f2[pp] - f1[u];
        else f2[u] = f1[pp] + f2[pp];
        if (f2[u] < 0) f2[u] = 0;
    }
    int sz = g[u].size();
    for (int i = 0; i < sz; i ++) {
        int v = g[u][i];
        if (v == pa[u]) continue;
        dfs2(v);
    }
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) {
        scanf("%d", a+i);
        if (!a[i]) a[i] = -1;
    }
    for (int i = 1; i < n; i ++) {
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs1(1);
    dfs2(1);
    for (int i = 1; i <= n; i ++) {
        if (i > 1) putchar(' ');
        int res = f1[i];
        if (f2[i] > 0) res += f2[i];
        printf("%d", res);
    }
    puts("");
    return 0;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/12486440.html