Ural1039. Anniversary Party

Background

The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to  N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

Problem

Your task is to make up a guest list with the maximal conviviality rating of the guests.

Input

The first line of the input contains a number  N. 1 ≤  N ≤ 6000. 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 the supervisor relation tree goes. Each line of the tree specification has the form
<L> <K>
which means that the  K-th employee is an immediate supervisor of  L-th employee. Input is ended with the line
0 0

Output

The output should contain the maximal total rating of the guests.

Sample

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


树形DP:

用f(x,flag) flag = 1 表示x的父亲节点被选择, flag = 0 表示未被选择

f(x,1) = f(son,0)

f(x,0) = max(f(son,1)+val[x],f(son,0))

此题存在一个隐形的坑点!就是当rating<0时就直接跳过不选!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 1000000000;
vector<int> vem;
const int maxn = 6000+10;
vector<vector<int> > tree;
int n;
int dp[maxn][2];
bool used[maxn];
void init(){
	for(int i = 1; i <= n; i++){
		dp[i][0] = dp[i][1] = INF;
	}
}
int dfs(int x,int flag){
	if(dp[x][flag]!=INF){
		return dp[x][flag];
	}
	if(flag==1){
		if(tree[x].size()==0){
			return 0;
		}
		int ans = 0;
		for(int i = 0; i < tree[x].size(); i++){
			ans += dfs(tree[x][i],0);
		}
		return dp[x][flag] = ans;
	}else{
		if(tree[x].size()==0){
			if(vem[x]<0)
				return 0;
			else
				return vem[x];
		}
		int a = 0,b=0;
		if(vem[x]<0){
			for(int i = 0; i < tree[x].size(); i++){
				b += dfs(tree[x][i],0);
			}
			return dp[x][flag] = b;
		}else{
			for(int i = 0; i < tree[x].size(); i++){
				a += dfs(tree[x][i],1);
				b += dfs(tree[x][i],0);
			}
			return dp[x][flag] = max(a+vem[x],b);
		}
	}
}
int main(){
	
    cin >> n;
	init();
    vem.resize(n+1);
    tree.resize(n+1);
    for(int i = 1; i <= n; i++)  scanf("%d",&vem[i]);
    int a,b;
    memset(used,0,sizeof(used));
    while(~scanf("%d%d",&a,&b) && a+b){
        tree[b].push_back(a);
        used[a] = 1;
    }
    int fa;
    for(int i = 1; i <= n; i++){
        if(!used[i])
            fa = i;
    }
	a = 0,b = 0;
	if(vem[fa]<0){
		for(int i = 0; i < tree[fa].size(); i++){
    		b += dfs(tree[fa][i],0);
    	}
    	cout<<b<<endl;
	}else{
		for(int i = 0; i < tree[fa].size(); i++){
			a += dfs(tree[fa][i],1);
    		b += dfs(tree[fa][i],0);
    	}
		cout<<max(a+vem[fa],b)<<endl;
	}
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值