A - Anniversary party

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

There are several cases. In each case, 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

需要注意是多组输入,如果不加最外面的while会WA。
每次开始前要把边数和数组清零,否则会溢出,导致Runtime Error(ACCESS_VIOLATION)。

f [ i ] [ 0 ] f[i][0] f[i][0]:表示结点 i i i不参加舞会时的最大值

f [ i ] [ 1 ] f[i][1] f[i][1]:表示结点 i i i参加舞会时的最大值

若以 i i i为根时, a n s = m a x ( f [ i ] [ 0 ] , f [ i ] [ 1 ] ) ans=max(f[i][0],f[i][1]) ans=max(f[i][0],f[i][1])

状态转移方程为:

f [ i ] [ 0 ] = ∑ m a x ( f [ x ] [ 0 ] , f [ x ] [ 1 ] ) f[i][0]=\sum max(f[x][0],f[x][1]) f[i][0]=max(f[x][0],f[x][1]) ( i i i不参加舞会时,下属随意)

f [ i ] [ 1 ] = ∑ f [ x ] [ 0 ] + a i f[i][1]=\sum f[x][0]+a_i f[i][1]=f[x][0]+ai i i i参加舞会时,下属不参加)

#include 
#include 
#include 
using namespace std;
const int N = 6e3 + 5; 

struct edge {//自己和兄弟结点间的边 
	int v, next;//next表示兄弟结点 
} e[N];

int head[N], n, cnt;//head表示第一个子结点,n表示结点数, 
int is_h[N], vis[N];//表示是否是子结点,是否访问过 
int f[N][2];

void addedge(int u, int v) {//建边 
	e[++cnt].v = v;
	e[cnt].next = head[u];//让v指向它的兄弟结点 
	head[u] = cnt;//使v成为u的第一个子节点 
}

void calc(int k) {
	vis[k] = 1;
	for (int i = head[k]; i; i = e[i].next)  //枚举该节点的每个子节点
	{
		if (vis[e[i].v])//被访问过 
			continue;
		calc(e[i].v);//递归 
		f[k][1] += f[e[i].v][0];//上司参加,下属只能不参加 
		f[k][0] += max(f[e[i].v][0], f[e[i].v][1]);//上司不参加,下属随意 
	}
	return;
}

int main(void) 
{
	int n;
	while (scanf("%d", &n) != EOF){
		cnt = 0;
		memset(head, 0, sizeof head);
		memset(is_h, 0, sizeof is_h);
		memset(vis, 0, sizeof vis);
		memset(f, 0, sizeof f);
		for (int i = 1; i <= n; i++) 
			scanf("%d", &f[i][1]);
		int l, k;
		while (scanf("%d%d", &l, &k) && l && k) {
			is_h[l] = 1;
			addedge(k, l);
		}
		for (int i = 1; i <= n; i++)
			if (!is_h[i]){  //从根节点开始DFS
				calc(i);
				printf("%d\n", max(f[i][1], f[i][0]));
				break;
			}
	}
	return 0;
}

你可能感兴趣的:(#,树形DP)