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): 8233    Accepted Submission(s): 3574

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

看题解说是树型dp,就学习了一下。

状态转移:

  上司去,下级不去:

    dp[node][1]+=dp[u][0];      //node去,则u必不能去

  上司不去,下级去或者不去:
    dp[node][0]+=max(dp[u][0],dp[u][1]);      //node不去,取u去或不去的最大值

和深搜很像,也就是用的深搜。

hdu上的数据更加刁钻,poj上能过的代码,在hdu上又是wa又是TLE。

开始模仿别人的代码用数组存上司和下级的关系,一直超时,最后改为用vector做.有的题解说可能不只一棵树,但是题目上说了是一棵树。

用了二维的vector,注意,每一组样例,都要清空vector,二维的需要每一维clear。用vector来搜索非常方便,节约时间和空间。

本题中vis数组没用。

注意理解树型dp思想。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<vector>
#include<iostream>
using namespace std;

int father[6005],vis[6005],dp[6005][2],t;
vector<int> v[6005];
void dfs(int node)
{
    int i,j;
    vis[node] = 1;
    for(i = 0; i<v[node].size(); i++)
    {
        int u=v[node][i];
        //if(!vis[u])
        //{
            dfs(u);
            dp[node][1]+=dp[u][0];//node去,则i必不能去
            dp[node][0]+=max(dp[u][0],dp[u][1]);//node不去,取i去或不去的最大值
        //}
    }
}

int main()
{
    int i,j,l,k,root;

    while(~scanf("%d",&t))
    {
        //memset(father,0,sizeof(father));

        for(i = 1; i<=t; i++)
        {
            v[i].clear();
            scanf("%d",&dp[i][1]);
            dp[i][0]=0;
            father[i]=0;
        }
        while(scanf("%d%d",&l,&k),l+k>0)
        {
            v[k].push_back(l);
            father[l]++;
        }
        //father[l] = k;//记录上司
        int ans=0;
        for(int i=1; i<=t; i++)
            if(father[i]==0)
            {
                memset(vis,0,sizeof(vis));
                dfs(i);
                ans+=max(dp[i][1],dp[i][0]);
            }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-10 08:48:46

HDU_1520_Anniversary party_树型dp的相关文章

HDU1561 The more, The Better(树型DP)

题目是有n个存有宝藏的城堡,攻克任何一个城堡都需要先攻克0个或其他1个城堡,问攻克m个城堡最多能得到多少宝藏. 题目给的城堡形成一个森林,添加一个超级根把森林连在一起就是树了,那么就考虑用树型DP: dp[u][m]表示以u结点为根的子树攻克m个结点的最大价值 但是这样转移太难了,根是从每个孩子通过各自分配若干的城堡去攻克转移的,一个排列组合数,阶乘,是指数级的时间复杂度! 看了题解,原来这是依赖背包,没看背包九讲..不过网上的博客似乎没说清楚,事实上这个状态应该是三个维度来表示: dp[u][

POJ3659 Cell Phone Network(树上最小支配集:树型DP)

题目求一棵树的最小支配数. 支配集,即把图的点分成两个集合,所有非支配集内的点都和支配集内的某一点相邻. 听说即使是二分图,最小支配集的求解也是还没多项式算法的.而树上求最小支配集树型DP就OK了. 树上的每个结点作为其子树的根可以有三个状态: 不属于支配集且还没被支配 不属于支配集但被其孩子支配 属于支配集 那么就是用dp[u][1\2\3]来表示动归的状态. 123转移该怎么转移就怎么转移..最后的结果就是min(dp[root][2],dp[root][3]). 要注意的是对于有些结点前2

HDU_1561_The more, The Better_树型dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1561 The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7031    Accepted Submission(s): 4121 Problem Description ACboy很喜欢玩一种战略游戏,

二叉苹果树(树型DP+背包)

二叉苹果树 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点).这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来描述一根树枝的位置.下面是一颗有4个树枝的树: 2   5 \  / 3  4 \  / 1 现在这颗树枝条太多了,需要剪枝.但是一些树枝上长有苹果. 给定需要保留的树枝数量,求出最多能留住多少苹果. 程序名:apple 输入格式: 第1行2个数,N和Q(1<=Q<= N,1<N<=

HDU_1011_Starship Troopers_树型dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16276    Accepted Submission(s): 4335 Problem Description You, the leader o

【HDOJ 5834】Magic boy Bi Luo with his excited tree(树型DP)

[HDOJ 5834]Magic boy Bi Luo with his excited tree(树型DP) Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Bi Luo is a magic boy, he also has a migic tree,

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each

[HAOI2010][BZOJ2427] 软件安装|tarjan|树型dp

2427: [HAOI2010]软件安装 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 463  Solved: 194[Submit][Status][Discuss] Description 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是现在有个问题:软件之间存在依赖关系,即软件i只有在安装了软件j(

HDU 1520Anniversary party(树型DP)

HDU 1520   Anniversary party 题目是说有N个人参加party,每个人有一个rating值(可以理解为权值)和一个up(上司的编号),为了保证party的趣味性,每一个人不可以和他的直接上司都参加,问最后的rating和最大 这是一个典型的树形DP,DP[i][0]表示i不参加那他的这棵子树上的最大权值,DP[i][1]表示i参加时的这棵树上的最大权值,那么: DP[i][0] = sum{MAX(DP[j][1], DP[j][0])  |  j是i的直接子节点} D