Hdoj 1520&Poj2342 Anniversary party 【树形DP】

Anniversary party

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

Total Submission(s): 5917 Accepted Submission(s): 2692

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

题意:有一个宴会要邀请一个公司的人来,这个公司有严格的等级制度,就是一颗树,现在树的每个节点都有一个(rating)权值,要求一个职员不能和他的直属boss(父节点)同时出现,求能够出现的最大的权值和。

思路:因为考虑到前面节点的最大值就要考虑到后面字节点的最大值,用dp。

状态方程:dp[i]【0】(代表第i个节点的不出现), dp[i][1]表示出现.

那么dp[i][0] += max(dp[j][0], dp[j][1]);

dp[i][1] += dp[j][0]; 其中j是i的子节点。

代码(HDoj的数据不是特别水不用vector会TL):

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int M = 6e3+5;

int dp[M][2], fat[M], n;
bool vis[M];
vector<int > map[M];

void dfs(int u){
    vis[u] = 1;
    for(int i = 0; i < map[u].size(); ++ i){
        int v = map[u][i];
        if(!vis[v]){
            dfs(v);
            dp[u][0] += max(dp[v][0], dp[v][1]);
            dp[u][1] += dp[v][0];
        }
    }
}

int main(){
    while(scanf("%d", &n) == 1){
        for(int i = 1; i <= n; ++i){
            dp[i][0] = dp[i][1] = 0;
            fat[i] = i;
            vis[i] = 0;map[i].clear();
        }
        for(int i = 1; i <= n; ++i){
            scanf("%d", &dp[i][1]);
        }
        int l, k;
        while(scanf("%d%d", &l, &k), l||k){
            if(fat[l] == l){
                fat[l] = k;
                map[k].push_back(l);
            }
        }
        int root = 1;
        while(root != fat[root]) root = fat[root];
        //memset(vis, 0, sizeof(vis));
        dfs(root);
        printf("%d\n", max(dp[root][0], dp[root][1]));
    }
    return 0;
}
时间: 2024-10-12 13:04:35

Hdoj 1520&Poj2342 Anniversary party 【树形DP】的相关文章

[poj2342]Anniversary party_树形dp

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

[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

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 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][

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 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&

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.

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

POJ Anniversary party 树形DP

/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能获得的最大权值和 dp[i][1]表示选择节点i ,同上! 转移方程: dp[i][0]+=max(dp[i_son][1],dp[i_son][0])如果没选择的话,那么子树可选择可不选择 dp[i][1]+=dp[i_son][0] 选择了之后,子树只能不选择 最后输出max(dp[i][0],