HDU1520 Anniversary party 树形DP基础

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.

InputEmployees 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 0OutputOutput 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

水题一道,晚安。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<memory>
using namespace std;
const int maxn=20000;

int vis[maxn],V[maxn],ans,cnt,dp[maxn][2];
int Laxt[maxn],Next[maxn],To[maxn],ru[maxn];

void add(int u,int v){
    Next[++cnt]=Laxt[u];
    Laxt[u]=cnt;
    To[cnt]=v;
}

int dfs(int u)
{
    vis[u]=1;
    dp[u][1]=V[u];
    for(int i=Laxt[u];i;i=Next[i]){
         int v=To[i];
         if(!vis[v]){
            dfs(v);
            dp[u][1]+=dp[v][0];
            dp[u][0]+=max(dp[v][0],dp[v][1]);
         }
    }
}

int main()
{
    int i,j,k,n,u,v;
    while(~scanf("%d",&n)){
        cnt=0;ans=0;
        memset(Laxt,0,sizeof(Laxt));
        memset(dp,0,sizeof(dp));
        memset(ru,0,sizeof(ru));
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++) scanf("%d",&V[i]);
        for(;;){
           scanf("%d%d",&u,&v);
           if(u==0&&v==0) break;
           add(v,u);
           ru[u]++;
        }
        for(i=1;i<=n;i++) {
          if(ru[i]==0) {
              dfs(i);
              ans+=max(dp[i][1],dp[i][0]);//防止多棵树
          }
         }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-02 00:47:51

HDU1520 Anniversary party 树形DP基础的相关文章

HDU-1520 Anniversary party (树形DP)

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.

HDU 1520:Anniversary party 树形DP基础

Anniversary party 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意: 公司里有一堆人再开派对,每个人有一个欢乐值,这些人之间有一些上下级关系,每个人都不想和他的直接上司(即是父亲,不包括祖先)同时出现在party上,求party上最大的欢乐值的合. 题解: 由题意知公司成员之间可以组成一棵关系树,设value[x]为以x为根节点的子树的最大权值合,child[x]为x的子节点,从树上可以看出,value[x]=max

POJ 2342 Anniversary party 树形DP基础题

题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人就不可以邀请其的直属员工, 问如何使得这个快乐值达到最大. 题解:对每个结点dp[i][0]表示不邀请这个员工,其子树达到的最大快乐值,dp[i][1]表示邀请i员工其子树达到的最大值. dp[i][0]=(i的全部员工的max(dp[u][1],dp[u][0)相加,也就是其子员工来或不来的最大快

poj 2324 Anniversary party(树形DP)

/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ---于是当i去时,i的所有儿子都不能去:dp[i][1]=sum(dp[j][0])+a[i],其中j是i的儿子节点. ---当i不去时,i的儿子可去也可不去:dp[i][0]=sum(max(dp[j][0],dp[j][1])),j是i的儿子节点 ---边界条件:当i时叶子节点时,dp[i][

hdu1520 第一道树形DP,激动哇咔咔!

A - 树形dp Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status 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

URAL 1039 Anniversary Party 树形DP 水题

1039. Anniversary Party Time limit: 0.5 secondMemory limit: 8 MB Background The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor rela

[poj2342]Anniversary party_树形dp

Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形dp 树形dp就是以节点或者及其子树为信息,进行动态规划.用dfs的原理,遍历,在回溯是更新父亲节点. 然后,关于这道题,我们就可以对于每一个节点进行标记,然后对于满足条件的节点进行遍历.设状态就是两个dp,分别表示选当前根节点和不选当前根节点.更新是瞎jb更新即可... .... 最后,附上丑陋的代码...

HDU 1520 Anniversary party (树形DP)

树形DP的关键在于如何处理递归返回的信息.这题dp[i][0]表示不选i点时当前最高权值.dp[i][1]表示选i点时当前最高权值.状态转移方程:dp[u][[0]+=max(dp[v][0],dp[v][1]),dp[u][1]+=dp[v][0]; 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm&

[poj2342]Anniversary party树形dp入门

题意:选出不含直接上下司关系的最大价值. 解题关键:树形dp入门题,注意怎么找出根节点,运用了并查集的思想. 转移方程:dp[i][1]+=dp[j][0];/i是j的子树 dp[i][0]+=max(dp[j][0],dp[j][1]); 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<iostream> 6