hdu 1520Anniversary party(简单树形dp)

hdu 1520Anniversary party(简单树形dp)

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

Anniversary party

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




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
 


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


题目大意:给你一颗树,是人际关系树,然后互为上下级的(父子节点)的不能同一时候选中。

解题思路:树形dp,任意从一个点開始扩展,把周围全部节点的dp都解决出来,然后加上去就可以。

用dp[i][0]表示不选中i号,周围的都能够选的最大值,dp[i][1]表示选中i号,那么周围都不能选的最大值。

dp[i][0]+=(dp[j][1],dp[j][0])

dp[i][1]+=dp[j][0]

i和j相邻,而且在求i的时候从j扩展的都已经求出来。


详见代码:


AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
const int maxn=6005;

vector<int> mq[maxn];
int dp[maxn][2];

void dfs(int cur,int p)
{
    int i,nex;
    for(i=0;i<mq[cur].size();i++)
    {
        nex=mq[cur][i];
        if(nex==p) continue;

        dfs(nex,cur);  //先找从cur这个点能够扩展的dp
        dp[cur][0]+=max(dp[nex][0],dp[nex][1]);
        dp[cur][1]+=dp[nex][0];
    }
}

int main()
{
    int n,i;
    int a,b;

    while(~scanf("%d",&n))
    {
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            mq[i].clear();
            scanf("%d",&dp[i][1]);
        }

        while(scanf("%d%d",&a,&b)&&(a+b))
        {
            mq[a].push_back(b);
            mq[b].push_back(a);
        }

        dfs(1,0);

        int ans=max(dp[1][0],dp[1][1]);
        printf("%d\n",ans);
    }

    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118610.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • LoadLibrary failed with error 1114:动态链接库(DLL)初始化例程失败 解决方法「建议收藏」

    LoadLibrary failed with error 1114:动态链接库(DLL)初始化例程失败 解决方法「建议收藏」LoadLibraryfailedwitherror1114:动态链接库(DLL)初始化例程失败解决方法

    2022年8月30日
    3
  • UpdatePanel的简单用法(非嵌套)「建议收藏」

    UpdatePanel的简单用法(非嵌套)「建议收藏」ScriptManager和UpdatePanel控件联合使用可以实现页面局部异步刷新的效果。UpdatePanel用来设置页面中局部异步刷新的区域,它必须依赖于ScriptManager,因为ScriptManager控件提供了客户端脚本生成与管理UpdatePanel的功能。

    2022年7月23日
    9
  • idea如何集成svn_集成吊顶步骤分解图

    idea如何集成svn_集成吊顶步骤分解图idea从项目窗口跳到打开项目选项窗口操作之后即可跳到如下界面第一步:下载svn的客户端,通俗一点来说就是小乌龟啦!官网下载地址:Downloads·TortoiseSVN下载之后直接安装就好了,但是要注意这里,选择安装所有的命令行客户端工具,默认是不安装的,如果不安装,svn中的bin目录下就会没有svn.exe,这个待会会用到,所以一点要注意哦。(都是坑啊)然后就下一步下一步就安装好了。第二步:如果已经搭建好了svn服务的话,就要开始在idea中配置相关.

    2022年8月31日
    5
  • JS 定义全局变量[通俗易懂]

    JS 定义全局变量[通俗易懂]JavaScript声明全局变量三种方式的异同JavaScript中声明变量格式:var(关键字)+变量名(标识符)。方式1vartest;vartest=5;需注意的是该句不能包含在function内,否则是局部变量。这是第一种方式声明全局变量。方式2test=5;没有使用var,直接给标识符test赋值,这样会隐式的声明了全局变量test。即使该语句是在一个func…

    2022年5月22日
    53
  • Oracle初学者-常用工具介绍

    Oracle初学者-常用工具介绍

    2021年8月16日
    51
  • 超分辨率重建开山之作——SRCNN

    论文及代码地址:http://mmlab.ie.cuhk.edu.hk/projects/SRCNN.html)基于卷积神经网络的影像超分辨率重建摘要:我们提出了一种基于深度学习的单影像超分辨率重建方法。我们直接以端对端的方式学习高…

    2022年4月6日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号