/** * @file 517P2386A.cpp * @author Zhoumouren * @brief * @version 0.1 * @date 2024-08-21 * * @copyright Copyright (c) 2024 * Set the dp[i][0/1] to calc node i choose (1) or not choose (0) the maximum we can get. */ #include<bits/stdc++.h> usingnamespace std;
structEdge { int to, nt; Edge() {} Edge(int to, int nt) : to(to), nt(nt) {} }e[20010]; int hd[6010], cnte; voidAddEdge(int u, int v){ e[++cnte] = Edge(v, hd[u]); hd[u] = cnte; }
int n; int a[6010];
int dp[6010][2];
voidInput(){ scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); } int u, v; for(int i = 1; i < n; i++) { scanf("%d%d", &u, &v); AddEdge(u, v); AddEdge(v, u); } }
voidDfs(int u, int fa){ for(int i = hd[u]; i; i = e[i].nt) { int v = e[i].to; if(v == fa) continue; Dfs(v, u); } dp[u][0] = 0; dp[u][1] = a[u]; for(int i = hd[u]; i; i = e[i].nt) { int v = e[i].to; if(v == fa) continue; dp[u][0] += max(dp[v][0], dp[v][1]); // if we not choose this point , we can choose or not choose it son dp[u][1] += dp[v][0]; // if we choose this point , we must do not choose it son. } }