树形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. nginx日志分析

    日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $b ...

  2. Queue 先进先出队列的操作

    1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...

  3. Unity3d5.0 新UI之2048

    因为汽车系统没写出来所以,纠结之中,弄了弄新版本的UI. 做了个2048. 新版本的unity的UI必须以Canvas为基底来呈现,如果没有加画布的话可是显示不出来东西的哦. 而且作为UI上的所有组件 ...

  4. PHPstorm激活

    最近想学习一下PHP 于是下载了很不错的phpstorm  但这老外的工具是要购买正版的 所以就搜了一下破解激活的教程 发现现在网上的在线破解在2016.2版本里面大多已被封杀 尝试了本地破解也发现大 ...

  5. Nginx反向代理tomcat

    location /gm {         root html;         proxy_pass http://localhost:8080/gm;         proxy_set_hea ...

  6. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  7. ubuntu sublime text3 lisence

    help --> enter lisence ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813 ...

  8. Ruby on rails学习笔记——安装环境

    出现问题: C:\Users\len>gem install rails ERROR: While executing gem ... (Gem::RemoteFetcher::FetchErr ...

  9. js中event的target和currentTarget的区别

    js中的event对象包含很多有用的信息 target:触发事件的元素. currentTarget:事件绑定的元素. 两者在没有冒泡的情况下,是一样的值,但在用了事件委托的情况下,就不一样了,例如: ...

  10. 设计模式学习——策略模式(Strategy Pattern)

    0. 前言 最近在重构公司的一个项目的时候,在抽取DES加密重复部分代码的时候,突然间想起了策略模式,感觉策略模式好像可以应用上,于是重新学习了下策略模式.注:在DES加密中,有DES和TDES算法, ...