hdu 1520 Anniversary party(第一道树形dp)

传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1520

Anniversary party

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16376    Accepted Submission(s): 6241

Problem 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

Source

Ural State University Internal Contest October‘2000 Students Session

Recommend

linle   |   We have carefully selected several similar problems for you:  1561 1011 2196 1494 2242

题目大意:

学校要开一个聚会。学校的教职工之间有上下级关系,为了让所有人开心,宴会组织者决定不会同时邀请一个人和他的上级(这让我想起我们昨天晚上聚餐李晔老师不来,她怕她来了我们放不开。。。。),对于每一个人,他能给聚会带来的欢乐度有一个值,问组织者该邀请哪些人能够使宴会的欢乐度达到最大值。

解题思路:

首先是DP的部分(也是很无聊的一部分):每个参与者都有两种状态,一种是参加,一种是不参加。这个状态的后续影响就是如果他参加了,他的直接上司和直接下属都不能参加。我们可以用一个二维二态的数组来描述:dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加。状态转移方程就是dp[i][1]=dp[i][1]+dp[i-1][0],dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1])。

本质:将一个数字三角形的操作放在了树上

第一道树形dp题,纪念一下,虽然对树的部分理解的还不是很透彻

code:

#include <iostream>
#include <stdio.h>
#include<memory>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
#define max_v 6005
struct node
{
    int pa,son;
    int next;
} point[max_v];

int dp[max_v][2];
//dp[i][1]表示第i个参与者参加了,dp[i][0]表示第i个参与者没有参加
//状态转移方程就:
//dp[i][1]=dp[i][1]+dp[i-1][0]
//dp[i][0]=dp[i][0]+Max(dp[i-1][0],dp[i-1][1])

int List[max_v];

int vis[max_v];//vis[a]=1 表示a有父节点

int value[max_v];//存值
int pos;

void add(int pa,int son)
{
    point[pos].pa=pa;
    point[pos].son=son;
    point[pos].next=List[pa];
    List[pa]=pos++;
}

void dfs(int root)
{
    if(List[root]==-1)//root没有子节点了
    {
        dp[root][1]=value[root];
        dp[root][0]=0;
        return ;
    }

    int now=List[root];
    dp[root][0]=0;
    dp[root][1]=value[root];

    while(now!=-1)
    {
        dfs(point[now].son);
        dp[root][1]+=dp[point[now].son][0];//既然取了父节点的值,子节点的值就不能再取了。

        //父节点的值没有取,子节点的值分取和不取两种情况,取其中较大的那种情况。
        dp[root][0]+=max(dp[point[now].son][1],dp[point[now].son][0]);

        now=point[now].next;//这个子节点计算过了,就要开始计算下一个子节点了
    }
    return ;
}

int main()
{
    int n;
    while(cin>>n)
    {
        for(int i=1; i<=n; i++)
            cin>>value[i];//记录每一个点的值

        memset(List,-1,sizeof(List));
        memset(vis,0,sizeof(vis));

        int a,b;
        pos=0;

        while(~scanf("%d %d",&a,&b))
        {
            if(a==0&&b==0)
                break;
            add(b,a);  //将边加入树中
            vis[a]=1; //记录a有父节点,不可能是祖节点。
        }

        a=1;
        while(vis[a]==1)
            a++;//找到根结点

        dfs(a);//从根结点开始搜

        printf("%d\n",max(dp[a][0],dp[a][1]));//取最大
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yinbiao/p/9406533.html

时间: 2024-10-09 15:34:41

hdu 1520 Anniversary party(第一道树形dp)的相关文章

hdu 1520 Anniversary party 生日party 树形dp第一题

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8667    Accepted Submission(s): 3748 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

HDU 1520.Anniversary party 基础的树形dp

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12770    Accepted Submission(s): 5142 Problem Description There is going to be a party to celebrate the 80-th Anniversary of the

hdu 1520 Anniversary party || codevs 1380 树形dp

Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical

hdu1520 第一道树形DP,激动哇咔咔!

A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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

HDU 1520 Anniversary party 树DP水题

非常水的树DP,状态为当前为i,上级来没来 然后跑一遍记忆化搜索即可 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib>

hdu 4405 概率dp 2012年金华亚洲网络赛--虽然水,但是是自己独立做的第一道概率dp

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4405 e[i]:当前在位置i还需要走的步数期望 受刘汝佳的AC自动机那个后缀链接写法的启发,我的x[i]通过逆序算出来连续有"flight line "的时候,能到达的最远距离, rep(i,0,m) { scanf("%d%d",&xx,&yy); x[xx]=yy; } for(int i=n;i>=0;i--) if(x[i]!=-1 &

hdu 1561The more, The Better(树形dp&amp;01背包)

The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4949    Accepted Submission(s): 2918 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

HDU 4008 Parent and son LCA+树形dp

题意: 给定case数 给定n个点的树,m个询问 下面n-1行给出树边 m个询问 x y 问:以x为根,y子树下 y的最小点标的儿子节点 和子孙节点 思路: 用son[u][0] 表示u的最小儿子 son[u][2] 表示u的次小儿子 son[u][1] 表示u的最小子孙 若lca(x,y)  !=y  则就是上述的答案 若lca(x,y) == y 1.y != 1 那么最小儿子就是除了x外的节点,且此时father[y] 也是y的儿子节点, 而最小的子孙节点就是1 2.y==1 那么特殊处理

HDU 4274 Spy&#39;s Work (树形DP,模拟)

题意: 给定一棵树,每个节点代表一个员工,节点编号小的级别就小,那么点1就是boss了.接下来给出对m个点的限制,有3种符号分别是op=“大于/小于/等于”,表示以第i个点为根的子树所有人的工资之和 大于/小于/等于 x,要求判断m个限制是否冲突了.注意每个员工的工资下限是1,而无上限.ps:可能出现对同个点多个限制,注意取交集. 思路: 很水的题嘛,想复杂了.注意限制是针对整棵子树的!所以我们只需要算出这棵子树的范围,再判断是否和所给的限制有冲突,如果没有冲突的话还得取“所给限制”与“计算出的