码迷,mamicode.com
首页 > 其他好文 > 详细

AYITACM2016省赛第三周 L - Anniversary party(树形dp)

时间:2016-05-07 10:36:33      阅读:237      评论:0      收藏: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

题意:

n个人(编号1-N),然后n行分表代表第n个人的活跃度,之后若干行 L 和 K(0 0结束),代表K是L的上司,

问一个聚会中邀请这n个人中的若干,其中不能含直接的上下级关系,可以使聚会中的活跃度最大为多少。

状态转移:

  上司去,下级不去:

    dp[c][1]+=dp[t][0];      //c去,则t必不能去

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

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

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>g[10001];
int dp[10001][3],root[10001];
void dfs(int c)
{
    int i,t;
    for(i=0;i<(int)g[c].size();i++)
    {
        t=g[c][i];//节点的孩子
        dfs(t);//把孩子作为节点再搜索它的孩子
        dp[c][1]+=dp[t][0];//如果双亲去,孩子就不能去
        dp[c][0]+=max(dp[t][0],dp[t][1]);//如果双亲不去,就选择孩子中欢乐值大的去
    }
}
int main()
{
    int n,i,a,b;
    while(scanf("%d",&n)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&dp[i][1]);//输入第i个人的活跃度
            root[i]=i;//初始化每个节点都是根节点,也是并查集的初始化
            g[i].clear();//清空容器
        }
        while(scanf("%d%d",&a,&b)&&a+b)
        {
            g[b].push_back(a);//把数据存入容器中
            root[a]=b;  //a的根节点为b
        }
        int beg;
        for(i=1;i<=n;i++)
            if(root[i]==i)//如果根节点是本身,说明就是树的根节点
         {
             beg=i;
            break;
        }
        dfs(beg);//搜索这棵树
        printf("%d\n",max(dp[beg][0],dp[beg][1]));//输出最终的欢乐值
    }
return 0;
}

AYITACM2016省赛第三周 L - Anniversary party(树形dp)

标签:

原文地址:http://blog.csdn.net/linyuxilu/article/details/51331464

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!