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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql优化的几种方法_sgd优化算法

    mysql优化的几种方法_sgd优化算法1.SGDBatchGradientDescent在每一轮的训练过程中,BatchGradientDescent算法用整个训练集的数据计算costfuction的梯度,并用该梯度对模型参数进行更新:Θ=Θ−α⋅▽ΘJ(Θ)\Theta=\Theta-\alpha\cdot\triangledown_\ThetaJ(\Theta)优点:costfuction若为凸函数,能

    2025年8月18日
    3
  • HashMap扩容流程[通俗易懂]

    HashMap扩容流程[通俗易懂]文章目录为什么扩容?什么时候扩容?如何扩容?今天在和同时讨论HashMap的时候,提到了扩容和冲哈希的事情,然后我发现大家都是一种半懂不懂的状态。于是回去做了一番功课,写下这篇文章。HashMap的扩容,又被很多人叫rehash、重哈希,我本人是很反对这个叫法的,事实上HashMap扩容的时候,Node中存储的Key的hash值并没有发生变化,只是Node的位置发生了变化。首先说为什么需要扩…

    2022年9月15日
    3
  • spel表达式的用法_el表达式判断是否为空

    spel表达式的用法_el表达式判断是否为空spel表达式运算使用翻看公司代码,这一块不是很懂,查资料,记一下,还是太菜1.常用的对象Expression:表达式对象SpelExpressionParser:表达式解析器EvaluationContext:上下文2.使用本文参考了下面的几篇文章https://www.cnblogs.com/shihuc/p/9338173.htmlhttps://blog.csdn.net/f641385712/article/details/90812967下面的例子主要是来源于第一

    2025年9月6日
    5
  • linux svn服务器搭建和配置_如何搭建web服务器

    linux svn服务器搭建和配置_如何搭建web服务器1.安装SVN服务器:检查是否已安装#rpm-qasubversion安装SVN服务器#yuminstallhttpdhttpd-develsubversionmod_dav_svnmod_auth_mysql验证安装#cd/etc/httpd/modules#ls|grepsvnmod_authz_svn.somod_dav_…

    2022年10月9日
    2
  • Drupal教程之安装篇

    Drupal教程之安装篇星期一,01/12/2009—似曾相识文章地址:[url]http://www.drupaluser.org/node/3[/url]象许多CMS一样,Drupal也是需要安装,其主要步骤如下(以[url]http://drupaluser.org/[/url]为例):1.在[url]http://drupaluser.org/[/u…

    2022年6月8日
    29
  • QT QMapIterator

    QT QMapIteratorQT的迭代器有两种类型:STL形式和JAVA形式。QT的STL形式的迭代器,和STL的迭代器用法类似,而JAVA形式的迭代器,则提供了一套迭代器类,用于QT容器的迭代。这其中,就有QT的迭代器类QMapIterator。QMapIterator的公共函数如下:QMapIterator(constQMap<Key,T>&map) bool fi…

    2022年5月30日
    74

发表回复

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

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