树形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. CentOS彻底卸载wine

    说明:折腾啊折腾,装了卸,卸了装,哈哈. 1. 卸载wine及相关组件 yum remove wine* 2. 删除相关文件 ~/.wine ~/.local/share/applications / ...

  2. pythong中字符串strip的用法

    strip的用法是去除字符串中前后两端的xx字符,xx是一个字符数组,并不是去掉“”中的字符串, 数组中包含的字符都要在字符串中去除.默认去掉空格,lstrip则是去掉左边的,rstrip是右边的 见 ...

  3. bootstrap学习总结-04 常用标签2

    1 表格 Bootstrap为表格设计了漂亮的样式. 1)表格基本实例 任意 <table> 标签添加 .table. <table class="table"& ...

  4. BZOJ1933: [Shoi2007]Bookcase 书柜的尺寸

    传送门 很容易看出来这是一道DP题,那么怎么设置状态就成了这道题的关键.本题有点特殊的地方是有两个维度的状态,而每个维度又有三个部分的参数,如果全部设置出来的话肯定会MLE.首先对书的厚度状态简化. ...

  5. spring---aop 配置

    第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步: 1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Befor ...

  6. [NOIP2014] 解方程&加强版 (bzoj3751 & vijos1915)

    大概有$O(m)$,$O(n\sqrt{nm})$,$O(n\sqrt{m})$的3个算法,其中后2个可以过加强版.代码是算法3,注意bzoj的数据卡掉了小于20000的质数. #include< ...

  7. JNI笔记

    由于要做一个能够加红字体的dialog,而cocos2d中的CCMessageBox是系统内带的,我无法修改其字体颜色.事实上是可以修改的,通过观察发现CCMessageBox被调用后,在安卓平台中会 ...

  8. TemplateDataField

    .aspx <ig:TemplateDataField Key="TemplateField_0"> <Header Text="selected&qu ...

  9. HTML学习笔记——块级标签、行级标签、图片标签

    1>块级标签.行级标签 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  10. Unixbench测试工具和使用

    安装过程 wget http://soft.laozuo.org/scripts/UnixBench5.1.3.tgz tar xf UnixBench5.1.3.tgz cd UnixBench5. ...