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

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

Ural State University Internal Contest October'2000 Students Session

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
#define N 6050
using namespace std;
vector<int >v[N];//用来建树
int w[N];//用来存放每个点的权值
int f[N];//用来找根节点在哪的
int dp[N][];//dp[i][j]表示第i个人来或不来的最大权值
void dfs(int root)
{
int len=v[root].size();//这个结点的儿子数
dp[root][]=w[root];
for(int i=;i<len;i++)
dfs(v[root][i]);//递归遍历下一层的儿子
for(int i=;i<len;i++)
{
dp[root][]+=max(dp[v[root][i]][],dp[v[root][i]][]);
dp[root][]+=dp[v[root][i]][];
}
}
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d",&n)!=EOF)
{
//cout<<n<<endl;
for(int i=;i<=n;i++)
{
scanf("%d",&w[i]);
dp[i][]=dp[i][]=;//这个人来或不来要清零
v[i].clear();
f[i]=-;
}
int a,b;
while(scanf("%d%d",&a,&b)!=EOF)
{
//cout<<a<<" "<<b<<endl;
if(a==&&b==) break;
f[a]=b;
v[b].push_back(a);//b结点有儿子a
}
int root=;
while(f[root]!=-)
{
//cout<<"root="<<root<<endl;
root=f[root];//找树根
}
//cout<<"root="<<root<<endl;
dfs(root);
printf("%d\n",max(dp[root][],dp[root][]));
}
return ;
}

hdu 1520 Anniversary party(入门树形DP)的更多相关文章

  1. HDU 1520 Anniversary party (树形DP,入门)

    题意:给一棵树,每个节点都有权值,要求选择部分节点出来,使得权值之和最大,但是每对(父亲,儿子)中最多只能挑一个. 思路: 比较入门的题,每个节点可以选也可以不选.若当前节点选的话,孩子必须全部不选: ...

  2. 题解报告:hdu 1520 Anniversary party(树形dp入门)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

  3. 【HDU】1520 Anniversary party(树形dp)

    题目 题目 分析 带权值的树上最大独立集 代码 #include <bits/stdc++.h> using namespace std; ; int a[maxn], n, fa[max ...

  4. hdu 1520 Anniversary party 基础树dp

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

  5. HDOJ 题目1520 Anniversary party(树形dp)

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

  6. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  7. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  8. poj2342 Anniversary party (树形dp)

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

  9. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

随机推荐

  1. ubuntu下pip的安装和使用

    对于python包的安装而言,需要pip包,对python包资源管理.pip包的安装.对于python2.x和python 3.x 方法不同 : Python 2: sudo dnf upgrade ...

  2. hdu2222 ac自动机入门

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

  3. java关键字中文对比

    abstract 摘要|抽象assert 声称boolean 布尔break 中断byte 字节case 实例catch 捕捉char 烧焦class 类const 常量continue 持续defa ...

  4. ZOJ2975 伪数组压缩+组合数

    Kinds of Fuwas Time Limit: 2 Seconds      Memory Limit:65536 KB In the year 2008, the 29th Olympic G ...

  5. easyUI tree点击文字展开节点

    easyUI默认展开树的时候,点击节点前边的黑色小三角 ,这样操作存在不人性化的地方,在实际使用中由于老旧电脑的存在和大龄使用者的眼花经常点不准:因此要实现点击节点名称展开的方式,其实就是在展开事件上 ...

  6. asp.net mvc CodeFirst模式数据库迁移步骤

    利用Code First模式构建好基本的类后,项目也开始搭建完毕并成功运行,而且已经将数据库表结构自动生成了. 但是,我有新的类要加入,有字段需要修改,那怎么办呢,删库,跑路 ?  哈哈 利用数据库迁 ...

  7. 平板不能设置代理的情况下利用随身wifi进行http代理访问

    需求来源:平板或手机是个封闭系统无法给wifi设置代理,需要利用filllder进行抓包,内容篡改等实验 拥有硬件资源:PC机器 + 小米随身wifi 方案1: NtBind Dns + Nginx ...

  8. 第五章 MySQL事务,视图,索引,备份和恢复

    第五章 MySQL事务,视图,索引,备份和恢复 一.事务 1.什么是事务 事务是一种机制,一个操作序列,它包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求.要么都执行 ...

  9. 【特效】页面滚动到相应位置运行css3动画

    请到我的个人博客网站上浏览此文章,欢迎评论和建议. 文章链接:http://www.xiaoxianworld.com/archives/87 现在css3动画很常见了,实际项目中经常应用,特别是那种 ...

  10. C#常用的字符串处理方法

    1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...