Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6635   Accepted: 3827

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 N – 1 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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = ;
int dp[N][]; //dp[i][0]表示当前i点不选 1表示选
int father[N],vis[N];
int n;
void creat(int m)
{
vis[m]=;
for(int i=;i<=n;i++)
{
if(vis[i]== && father[i]==m)
{
creat(i); //cout<<m<<endl;
dp[m][]+=max(dp[i][],dp[i][]);//m不去,取i去或不去的最大值
dp[m][]+=dp[i][];//m去,则i必不能去
}
}
}
int main()
{
int i;
while(~scanf("%d",&n))
{
Del(dp,);Del(father,);
Del(vis,);
for(i=; i<=n; i++)
{
scanf("%d",&dp[i][]);
}
int f,c,root;
root = ;//记录父结点
bool beg = ;
while (scanf("%d %d",&c,&f),c||f)
{
father[c] = f;
if( root == c || beg )
{
root = f;
}
}
while(father[root])//查找父结点
root=father[root];
creat(root);
int imax=max(dp[root][],dp[root][]);
printf("%d\n",imax);
}
return ;
}

POJ2342 Anniversary party(动态规划)(树形DP)的更多相关文章

  1. poj2342 Anniversary party (树形dp)

    poj2342 Anniversary party (树形dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9128   ...

  2. poj2342 Anniversary party【树形dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316097.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  3. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  4. 动态规划——树形dp

    动态规划作为一种求解最优方案的思想,和递归.二分.贪心等基础的思想一样,其实都融入到了很多数论.图论.数据结构等具体的算法当中,那么这篇文章,我们就讨论将图论中的树结构和动态规划的结合——树形dp. ...

  5. Anniversary party (树形DP)

    There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The Un ...

  6. hdu1520 Anniversary party 简单树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 思路:树形DP的入门题 定义dp[root][1]表示以root为根节点的子树,且root本身参 ...

  7. HDU1520:Anniversary party(树形dp第一发)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1520 一个公司去参加宴会,要求去的人不能有直接领导关系,给出每一个人的欢乐值,和L K代表K是L的直接领导 ...

  8. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  9. hdu 1520 Anniversary party(入门树形DP)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6926   Accepted: 3985 ...

随机推荐

  1. 【题解】SDOI2014旅行

    洛谷P3313 大概是一道树链剖分的裸题.可以看出如果不是查询相同宗教的这一点,就和普通的树链剖分毫无两样了.所以针对每一个宗教都单独开一棵线段树,变成单点修改+区间查询.只不过宗教数目很多,空间消耗 ...

  2. [CF1065C]Make It Equal

    题目大意:$n$列箱子,横向消除,一次可以把一行及以上的所有箱子消除,但是一次最多只可以消除$k$个,求最少用几次把箱子的高度变成一样 题解:贪心,求出比一个高度高的有几个箱子,消除即可 卡点:代码改 ...

  3. python实现关联规则

    代码中Ci表示候选频繁i项集,Li表示符合条件的频繁i项集 # coding=utf-8 def createC1(dataSet): # 构建所有1项候选项集的集合 C1 = [] for tran ...

  4. BZOJ2460 [BeiJing2011]元素 【线性基】

    2460: [BeiJing2011]元素 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 1675  Solved: 869 [Submit][St ...

  5. mybatis学习(七)——resultType解析

    resultType是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,List类型,Map类型等.现总结一下再解释 总结: resultType: 1.基本类型  :resultType= ...

  6. JAVA int自动装箱

    int 转 Integer: Integer int127_1 = 127; Integer int127_2 = 127; System.out.println("int127_1 == ...

  7. [MySQL] explain执行计划解读

    Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...

  8. ServletContext 接口读取配置文件要注意的路径问题

    在建立一个maven项目时,我们通常把一些文件直接放在resource下面,在ServletContext中有getResource(String path)和getResourceAsStream( ...

  9. Kafka自我学习3-Scalable

    1.After created the zookeeper cluster, we found all broker cluster topic can be find in zoo1, zoo2, ...

  10. linux网络编程系列-TCP/IP模型

    ### OSI:open system interconnection ### 开放系统互联网模型是由ISO国际标准化组织定义的网络分层模型,共七层 1. 物理层:物理定义了所有电子及物理设备的规范, ...