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


相关推荐

  • 如何设置双屏壁纸(无需通过软件实施)「建议收藏」

    如何设置双屏壁纸(无需通过软件实施)「建议收藏」如何设置双屏壁纸1.先下载两张壁纸2.选中两张壁纸二级目录三级目录1.先下载两张壁纸将下载两张壁纸到同一个文件夹下2.选中两张壁纸鼠标右击,在右击菜单中选择设置为桌面背景二级目录三级目录…

    2025年8月14日
    2
  • pytest运行_ios怎么清理应用缓存在哪里

    pytest运行_ios怎么清理应用缓存在哪里前言pytest运行完用例之后会生成一个.pytest_cache的缓存文件夹,用于记录用例的ids和上一次失败的用例。方便我们在运行用例的时候加上–lf和–ff参数,快速运行上一

    2022年7月31日
    3
  • fflush和fsync的联系和区别「建议收藏」

    fflush和fsync的联系和区别「建议收藏」1.提供者fflush是libc.a中提供的方法,fsync是系统提供的系统调用。2.原形fflush接受一个参数FILE*.fflush(FILE*);fsync接受的时一个Int型的文件描述符。fsync(intfd);3.功能fflush:是把C库中的缓冲调用write函数写到磁盘[其实是写到内核的缓冲区]。fsync:是把内核缓冲刷到磁盘上。c库

    2022年5月13日
    39
  • python 如何使用swagger

    swagger介绍swagger是一个api文档工具,集api管理,测试,访问于一体的网页版api文档工具了解更多,请访问相关网站swagger官网swaggergithubOpenApi参数说明python相关包connexionflasggerflask-swag,flask-swaggerFlask-RESTPluspythonswagger-cod…

    2022年4月12日
    698
  • Linux运维常见面试题汇总

    Linux运维常见面试题汇总Linux面试题一、填空题1. 在Linux 系统中,以文件方式访问设备。2.Linux 内核引导时,从文件/etc/fstab中读取要加载的文件系统。3.Linux 文件系统中每个文件用indoe节点来标识。4. 全部磁盘块由四个部分组成,分别为引导块 、专用块 、 i 节点表块 和 数据存储块 。5. 链接分为:硬链接 和 符号链接 。

    2022年6月13日
    19
  • 评教,路上的风景更美「建议收藏」

    评教,路上的风景更美

    2022年1月30日
    35

发表回复

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

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