Description:

There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests’ conviviality ratings.

Input:

Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0

Output:

Output should contain the maximal sum of guests’ ratings.

Sample Input:

7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

Sample Output:

5

题目链接

树型dp入门题目。

树型dp顾名思义即为在树状结构上进行的dp(动态规划),下面AC代码采用由叶子节点至根节点的递推更新方向,先找到根节点,在dfs中递归到叶子节点再向上回溯更新信息。

此题为一棵树上每个节点有一个权值,子节点和父节点不能同时选择,求选择节点之后最大的权值之和。

由下向上递推,依次存储是否选择当前节点的最优解。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
#define XDebug(x) cout<<#x<<"="<<x<<endl;
#define ArrayDebug(x,i) cout<<#x<<"["<<i<<"]="<<x[i]<<endl;
#define print(x) out(x);putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 6e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
template <class T>
inline bool read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) {
        return 0;
    }
    while (c != '-' && (c < '0' || c > '9')) {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') {
        ret = ret * 10 + (c - '0');
    }
    ret *= sgn;
    return 1;
}
template <class T>
inline void out(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) {
        out(x / 10);
    }
    putchar(x % 10 + '0');
}

int n;
int u, v;
int Root;
int HappyVal[maxn];
int f[maxn];
int dp[maxn][2];
vector<int> Adj[maxn];

/* * dp[u][0/1]表示u子树内的最佳答案,由叶子节点向根节点递推 * dp[u][0]代表选择u节点,dp[u][1]代表不选择u节点 * 初始化dp[u][0]=0,dp[u][1]=HappyVal[u] * 状态转移方程: * 1.当u不选的时候:u的子节点可以选或者不选,所以dp[u][0] += max(dp[v][0], dp[v][1]) * 2.当u选的时候:u的子节点必定不能选,所以dp[u][1] += dp[v][0] */

void dfs(int root) {
	dp[root][1] = HappyVal[root];
	for (int i = 0; i < int(Adj[root].size()); ++i) {
		dfs(Adj[root][i]);
	}
	for (int i = 0; i < int(Adj[root].size()); ++i) {
		dp[root][0] += max(dp[Adj[root][i]][1], dp[Adj[root][i]][0]);
		dp[root][1] += dp[Adj[root][i]][0];
	}
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
	while (read(n)) {
		for (int i = 1; i <= n; ++i) {
			Adj[i].clear();
			read(HappyVal[i]);
			f[i] = -1;
			dp[i][0] = dp[i][1] = 0;
		}
		while (read(v) && read(u) && (v + u)) {
			Adj[u].pb(v);
			f[v] = u;
		}
		// 找树根
		Root = 1;
		while (f[Root] != -1) {
			Root = f[Root];
		}
		dfs(Root);
		print(max(dp[Root][0], dp[Root][1]));
	}
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}