POJ 2342 Anniversary party(树型DP入门题)

9 篇文章 0 订阅
2 篇文章 0 订阅

题目链接

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 N – 1 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

PS:题意:一棵树每个节点都有自己的权值,现在要求选一些节点使权值之和最大,选节点的要求是要是选了该节点,该节点的父节点和子节点都不能选。

题解:首先声明一个二维的dp数组,dp[i][0]表示i节点不选,dp[i][1]表示i节点要选。这样我们很容易得出状态转移方程。

当u节点不选的时候,u节点的子节点都可以选,也可以不选,所以加上两者当中较大的,dp[u][0]+=max(dp[v][0],dp[v[[1])。(v为u的子节点)

当u节点选的时候,他的子节点就不能选了,dp[u][1]+=dp[v][0];

做的时候我们首先要找到根节点,然后用dfs遍历。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=6e3+10;
const int mod=10007;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
struct node
{
    int v,next;
} rea[maxn<<1];
int head[maxn],len,root[maxn],dp[maxn][maxn];
void inct()
{
    me(head,-1),len=0;
}
void add(int u,int v)
{
    rea[len].v=v;
    rea[len].next=head[u];
    head[u]=len++;
}
int dfs(int u)//遍历
{
    for(int i=head[u]; i!=-1; i=rea[i].next)
    {
        int v=rea[i].v;
        dfs(v);
        dp[u][1]+=dp[v][0];
        dp[u][0]+=max(dp[v][1],dp[v][0]);
    }
    return max(dp[u][0],dp[u][1]);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        inct();//每次都要初始化 。
        for(int i=1; i<=n; i++)
        {
            dp[i][0]=0,root[i]=0;//要是给节点不选的话,初始化为0,选的话,初始化为该节点的权值。
            scanf("%d",&dp[i][1]);
        }
        int x,y;
        while(scanf("%d%d",&x,&y)&&x+y)
        {
            add(y,x);///存图
            root[x]++;
        }
        int ans;
        for(int i=1; i<=n; i++)
            if(!root[i])//找到根节点
                ans=i;
        dfs(ans);
        printf("%d\n",max(dp[ans][0],dp[ans][1]));//因为不确定根节点是否要选,所以直接输出选或不选中的较大值。
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值