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

Ural State University Internal Contest October‘2000 Students Session

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扩展的都已经求出来。

详见代码:

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1520

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;
}

hdu 1520Anniversary party(简单树形dp),布布扣,bubuko.com

时间: 2024-11-08 20:13:54

hdu 1520Anniversary party(简单树形dp)的相关文章

HDU 1520-Anniversary party(树形dp入门)

题意: n个人参加party,已知每人的欢乐值,给出n个人的工作关系树,一个人和他的顶头上司不能同时参加,party达到的最大欢乐值. 分析:dp[i][f],以i为根的子树,f=0,i不参加,f=1,i参加 能达到的最大欢乐值.i参加i的孩子不能参加,i不参加,其孩子参不惨加都行(取最大值). #include <map> #include <set> #include <list> #include <cmath> #include <queue&

csu 2014 summer training day 3 简单树形dp

FZU 2157 题意:树上的节点可以打上0或1的标记,树的权值由两部分呢组成,点权和边权,有00.01.10.11四种组合的边权, 问最小权值和.以1节点为树根 分析:dp[x][0]表示x标记0后的最小的权值,dp[x][1]同理 那么每次可以计算dp[x][0],dp[x][1]: 例如dp[x][1]=min(dp[son][0]+lab[0][1]+val[1],dp[son][1]+bal[1][1]+val[1]); 具体看代码. 用bfs写的分层,深搜代码更简洁 1 #inclu

HDU 1011 Starship Troopers(树形DP)

Starship Troopers Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 62   Accepted Submission(s) : 12 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description You, the leader of

hdu 4044 GeoDefense (树形dp+01背包)

GeoDefense Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 663    Accepted Submission(s): 267 Problem Description Tower defense is a kind of real-time strategy computer games. The goal of towe

sgu 143 Long live the Queen 简单树形dp

// sgu 143  Long live the Queen 简单树形dp // // 题意:在树上选一个连通字图,使得节点的权值之和最大 // f[i] 表示以该节点为根的字图权值之和的最大值 // 则有 f[i] = w[i] + sigma(max(0,f[j])) i是j的父节点 // 最后在所有的f中挑个最大值就是答案.... #include <algorithm> #include <bitset> #include <cassert> #include

HDU 2196 Computer 经典树形DP

一开始看错题了,后来发现原来是在一颗带权的树上面求出距离每一个点的最长距离,做两次dfs就好,具体的看注释? #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #incl

HDU 3899 简单树形DP

题意:一棵树,给出每个点的权值和每条边的长度, 点j到点i的代价为点j的权值乘以连接i和j的边的长度.求点x使得所有点到点x的代价最小,输出 虽然还是不太懂树形DP是什么意思,先把代码贴出来把. 这道题目的做法是:先进行一次DFS,以每个节点为根,求出它下面节点到它的数量和. 再进行一次DFS,以每个节点为根,求出它下面节点到它的花费总和. source code: #pragma comment(linker, "/STACK:16777216") //for c++ Compile

POJ 2342 &amp;&amp;HDU 1520 Anniversary party 树形DP 水题

一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点)都不能被邀请 2.每一个员工都有一个兴奋值,在满足1的条件下,要使得邀请来的员工的兴奋值最高 输出最高的兴奋值. 简单的树形DP dp[i][1]:表示以i为根的子树,邀请节点i的最大兴奋值 dp[i][0]:表示以i为根的子树,不邀请节点i的最大兴奋值 先根据入度找出整棵树的根节点, 然后一次DF

hdu 5452 Minimum Cut 树形dp

Minimum Cut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5452 Description Given a simple unweighted graph G (an undirected graph containing no loops nor multiple edges) with n nodes and m edges. Let T be a spa