Each node has the right value, the child node and the parent node cannot be selected at the same time, what is the maximum value that can be selected at the end?
#include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath > #include <cstring>using namespace std;typedef long long int64;const int INF = 0x3f3f3f3f;const Double PI = ACOs ( -1.0); const int MAXN = 6010;vector<int>adj[maxn];int Indeg[maxn];int val[maxn];int f[maxn][2];int N, m;void Dfs ( int u) {f[u][0] = 0; F[U][1] = Val[u]; for (int i=0; i<adj[u].size (); ++i) {int v = adj[u][i];//if (vis[v]) continue; DFS (v); F[u][0] + = max (f[v][1], f[v][0]); F[U][1] + = f[v][0]; }}int Main () {while (~SCANF ("%d", &n) && N) {for (int i=1; i<=n; ++i) adj[i].clear (); for (int i=1; i<=n; ++i) scanf ("%d", &val[i]); memset (indeg, 0, sizeof (INDEG)); int u, v; while (~SCANF ("%d%d", &v, &u) && v+u) {adj[u].push_back (v); ++INDEG[V]; } memset (F, 0, sizeof (f)); for (int i=1; i<=n; ++i) if (!indeg[i]) {DFS (i); printf ("%d\n", Max (F[i][0], f[i][1])); Break }} return 0;}
hdu1520 Tree DP