HDU 1520 Anniversary party(DFS或树形DP)

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

题意:n个人去参加聚会,其中有直接上下级关系的参加聚会会影响气氛,所以不能参加,每个人参加聚会都会有一个活跃度,现在要你计算从n个人中选择出一些人,使得本次聚会的活跃度最大

思路:很简单,每个人参加和不参加都会有不同的结果,他参加,那么只需要加上他的下属不参加的权值,他不参加,那么加上他下属参加或不参加中的最大值,最后只需要计算出最高上司参加或者不参加的最大值就可以了

PS:树形DP的代码你们去参考其他人的吧,最近在训练蓝桥杯,所以强化DFS能力

AC代码:

#include<cstdio>

#define N 6005

struct p
{
    int fm;
    int child;
    int brother;
    int att;
    int no;
    void init()
    {
        no=0;
        fm=0;
        brother=0;
        child=0;
    }
    int Max()
    {
        return att>no? att:no;
    }
}num[N];

void dfs(int x)
{
    int child=num[x].child;
    while(child)
    {
        dfs(child);
        num[x].att+=num[child].no;
        num[x].no+=num[child].Max();
        child=num[child].brother;
    }
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            num[i].init();
            scanf("%d",&num[i].att);
        }
        int a,b;
        while(scanf("%d %d",&a,&b),a||b)
        {
            num[a].fm=b;
            num[a].brother=num[b].child;
            num[b].child=a;
        }
        for(int i=1;i<=n;i++)
        {
            if(num[i].fm==0)
            {
                dfs(i);
                printf("%d\n",num[i].Max());
                break;
            }
        }
    }
    return 0;
}
时间: 2024-10-07 11:52:14

HDU 1520 Anniversary party(DFS或树形DP)的相关文章

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 生日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 || 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

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 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:可能出现对同个点多个限制,注意取交集. 思路: 很水的题嘛,想复杂了.注意限制是针对整棵子树的!所以我们只需要算出这棵子树的范围,再判断是否和所给的限制有冲突,如果没有冲突的话还得取“所给限制”与“计算出的

hdu 1011 Starship Troopers (依赖背包 树形dp)

题目: 链接:点击打开链接 题意: n个房间组成一棵树,你有m个战队,从1号房间开始依次clear每个房间,在每个房间需要花费的战队个数是bugs/20,得到的价值是the possibility of capturing a brain,求最大的价值. 算法: 树形dp,有依赖的背包问题.(依次clear每个房间) 思路: 状态转移dp[i][j]表示根结点为i时(房间i)花费j个战队能够得到的最大价值(捕捉到一个brain最大的可能值).递归求出每个根结点处的最大值,最后dp[1][m]就是

HDU 1561The more, The Better(树形DP)

HDU 1561  The more, The Better 题目大意就不说了 直接DP[i][j]表示i为跟节点的子树上攻克j个城堡的所能获得的最多宝物的数量 DP[fa][j] = MAX{DP[fa][i-k] + DP[child][k]}; 首先一个问题就是说如果子树u下的任意子节点被选择了,那么u是一定需要选择的,怎么在DP时保证准确性,其实这个很好解决,我们在计算时是需要枚举k(子节点的攻克数量)的,那么我们迫使k<j就可以了,这样的话DP[fa][j] 就不会被子节点的DP[ch

poj3107(dfs,树形dp)

和poj1655的方法完全一样,但是这道题的n的范围大了,用vector存图会TLE,所以改用前向星来存图就可以了. 这里解释一下前向星存图的方法: 其实就是用静态链表来实现邻接链表,这样可以避免使用指针. head[i]数组来记录每个节点的第一条边:每条边用结构体e[i]来存,e[i].v表示这条边指向的点,e[i].next表示这条边连向的下一条边. 它的巧妙之处在于每次插入到链表的首部而不是尾部,这样就避免了对链表的遍历.同一起点的各条边在邻接表中的顺序和读入顺序正好相反. 贴个模板: s