树形DP。。。。在树上做DP。。。。不应该是猴子干的事吗? 

Anniversary party

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

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
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int valu[7000],dp[7000][2],N,L,K;
bool isson[7000];
vector<int> g[7000];

void dfs(int u,int fa)
{
    //printf("%d--->%d\n",u,g.size());
    for(int i=0;i<g.size();i++)
    {
        int v=g;
        if(v==fa) continue;
        dfs(v,u);
    }
    int qu=0,buqu=0;
    if(g.size()==1)
    {
        //printf("...%d\n",u);
    }
    for(int i=0;i<g.size();i++)
    {
        int v=g;
        if(v==fa) continue;
        qu=max(qu,qu+dp[v][0]);
        buqu=max(buqu+dp[v][1],max(dp[v][0]+buqu,buqu));
    }
    dp[0]=buqu;
    dp[1]=qu+valu;
}

int main()
{
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=1;i<=N;i++)
        {
            scanf("%d",valu+i);
            g.clear();
        }
        memset(isson,false,sizeof(isson));
        memset(dp,0,sizeof(dp));
        while(scanf("%d%d",&L,&K)!=EOF)
        {
            if(L==0&&K==0) break;
            isson[L]=true;
            g[L].push_back(K);
            g[K].push_back(L);
        }
        int root=-1;
        for(int i=1;i<=N;i++)
        {
            if(!isson)
            {
                root=i; break;
            }
        }
        dfs(root,-1);
        printf("%d\n",max(dp[root][1],dp[root][0]));
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

HDOJ 1520 Anniversary party的更多相关文章

  1. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  2. hdu 1520 Anniversary party(第一道树形dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Time Limit: 2000/1000 MS (Java ...

  3. HDOJ 题目1520 Anniversary party(树形dp)

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. 【HDOJ】1520 Anniversary party

    第二道树形DP,先是MLE.后来仅需改小邻接矩阵的第二个维度到30就过了. #include <cstdio> #include <cstring> #include < ...

  5. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. hdu 1520 Anniversary party 基础树dp

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  8. hdu 1520 Anniversary party || codevs 1380 树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. [HDU 1520] Anniversary party

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. MYSQLDUMP参数详解

    mysqldump客户端可用来转储数据库或搜集数据库进行备份或将数据转移到另一个SQL服务器(不一定是一个MySQL服务器).转储包含创建表和/或装载表的SQL语句. 如果你在服务器上进行备份,并且表 ...

  2. resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found.

    resultset 对象获取行字段数据时报:java.sql.SQLException: Column 'id' not found. 代码: String sql="SELECT d.co ...

  3. 个人作业—Week2:微软必应词典案例分析

    调研.评测 bug报告: 标题:Window 10版必应词典客户端口语练习功能无法使用 环境:Window 10, 微软必应词典(UWP) 版本2.6.1.0,屏幕无重力感应模块 重现步骤: 1)   ...

  4. 全栈必备 Linux 基础

    Linux 几乎无处不在,不论是服务器构建,还是客户端开发,操作系统的基础技能对全栈来说都是必备的.系统的选择Linux发行版本可以大体分为两类,一类是商业公司维护的发行版本,一类是社区组织维护的发行 ...

  5. bookstrap必备的基础知识

  6. netty socket 客服端编程

    package com.ming.netty.nio; 2 3 import io.netty.bootstrap.Bootstrap; 4 import io.netty.channel.Chann ...

  7. 查看apt-get安装软件的版本

    apt-cache search name 查询 apt-get install name 安装 dpkg dpkg dpkg-checkbuilddeps dpkg-genchanges dpkg- ...

  8. Android学习笔记——ListView

    该工程的功能是实现在一个activity中显示一个列表 以下代码是MainActivity.java中的代码 package com.example.listview; import java.uti ...

  9. Ad-Hoc命令不熟悉的选项

    -f #并发线程数,默认5个线程 --private-key #指定秘钥文件 -k #--ask-pass SSH:认证密码 -K, #--ask-sudo-pass sudo:用户的密码(--sud ...

  10. Unity 播放 视频

    Unity3D 播放视频 http://www.cnblogs.com/fortomorrow/archive/2012/11/01/unity07.html Unity3D研究院之两种方式播放游戏视 ...