Anniversary party
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5523   Accepted: 3169

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

Source

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int f[][],used[],father[];
void treee(int num)
{
int i;
used[num]=;
for (i=;i<=n;i++)
{
if (!used[i] && father[i]==num)
{
treee(i);
f[num][]+=f[i][];
f[num][]+=max(f[i][],f[i][]);
}
}
}
int main()
{
int root,a,b,bb;
int i;
while (~scanf("%d",&n))
{
memset(father,,sizeof(father));
memset(used,,sizeof(used));
memset(f,,sizeof(f));
for (i=;i<=n;i++)
scanf("%d",&f[i][]);
root=;
bb=;
while (scanf("%d%d",&a,&b),a||b)
{
father[a]=b;
if (a==root || bb==)
{
root=b;
}
}
while (father[root])
root=father[root];
treee(root);
printf("%d",max(f[root][],f[root][]));
}
return ;
}

【Poj】 p2342 Anniversary party(树形DP第一道)的更多相关文章

  1. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  2. POJ 2342 - Anniversary party - [树形DP]

    题目链接:http://poj.org/problem?id=2342 Description There is going to be a party to celebrate the 80-th ...

  3. poj 2342 Anniversary party 树形DP入门

    题目链接:http://poj.org/problem?id=2342 题意:一家公司有1 <= N <= 6 000个职工,现要组织一些职工参加晚会,要求每个职工和其顶头上司不能同时参加 ...

  4. POJ 2342 Anniversary party 树形DP基础题

    题目链接:http://poj.org/problem?id=2342 题目大意:在一个公司中,每个职员有一个快乐值ai,现在要开一个party,邀请了一个员工就不可能邀请其直属上司,同理邀请了一个人 ...

  5. POJ 2342 &&HDU 1520 Anniversary party 树形DP 水题

    一个公司的职员是分级制度的,所有员工刚好是一个树形结构,现在公司要举办一个聚会,邀请部分职员来参加. 要求: 1.为了聚会有趣,若邀请了一个职员,则该职员的直接上级(即父节点)和直接下级(即儿子节点) ...

  6. TTTTTTTTTTT hdu 1520 Anniversary party 生日party 树形dp第一题

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

  7. POJ Anniversary party 树形DP

    /* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...

  8. POJ 3162 Walking Race 树形DP+线段树

    给出一棵树,编号为1~n,给出数m 漂亮mm连续n天锻炼身体,每天会以节点i为起点,走到离i最远距离的节点 走了n天之后,mm想到知道自己这n天的锻炼效果 于是mm把这n天每一天走的距离记录在一起,成 ...

  9. POJ 2342 Anniversary party (树dp)

    题目链接:http://poj.org/problem?id=2342 有n个人,每个人有活跃值.下面n-1行u和v表示u的上司是v,有直接上司和下属的关系不能同时参加party,问你party最大的 ...

  10. [poj2342]Anniversary party_树形dp

    Anniversary party poj-2342 题目大意:没有上司的舞会原题. 注释:n<=6000,-127<=val<=128. 想法:其实就是最大点独立集.我们介绍树形d ...

随机推荐

  1. 浅析配置更快的Eclipse方法

    很多人感觉自己的elipse启动比较慢,其实并不是因为装的插件太多或者是导入的项目有点大,而是因为参数的设置不合理导致的.可以在eclipse.ini里面添加-Xloggc:gc.log看看启动的日志 ...

  2. ubuntu wubi非在线快速安装

    最近ubuntu更新了,就想把它重新装回来试一下,但是由于种种原因划分磁盘不太方便,很自然就想到了wubi,这个不仅仅安全性高,而且比直接装系统快多了,而且方便.但是在线安装实在是太慢了,所以就找到了 ...

  3. hdu 4049 2011北京赛区网络赛J 状压dp ***

    cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...

  4. JSON资料汇总

    网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...

  5. Silverlight中的TabControl如何绑定数据?重写tabcontrol和tabItem 解决绑定友好问题。可以绑定对象集合

    在 WPF 中,TabControl 可以直接将 ItemsSource 绑定数据源,见 将 TabControl 绑定到数据的示例 http://msdn.microsoft.com/zh-cn/l ...

  6. Servlet请求头response应用简单案例

    Servlet请求头response应用简单案例:访问AServlet重定向到BServlet,5秒后跳到CServlet,并显示图片: AServlet package cn.yzu; import ...

  7. 纯css实现两列等高

    <!doctype html> <html> <head> <meta /> <title>Title</title> < ...

  8. css整理-05 边框,背景和浮动,定位

    边框 样式:border-style hidden, dotted, dashed, solid , double, groove, ridge, inset, outset 最不可预测的是doubl ...

  9. 分享Kali Linux 2016.2第41周镜像虚拟机

    分享Kali Linux 2016.2第41周镜像虚拟机该虚拟机使用Kali Linux 2016.2第41周镜像文件安装而成,系统已经更新到10月12日.里面已经进行如下配置:(1)设置官方软件源( ...

  10. MFC HTTP访问URL

    unsigned short nPort; //用于保存目标HTTP服务端口 CString strServer, strObject; //strServer用于保存服务器地址,strObject用 ...