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. js Date() 浏览器兼容问题解决

    一般 直接new Date() 是不会出现兼容性问题的,而 new Date(datetimeformatstring) 常常会出现浏览器兼容性问题,为什么,datetimeformatstring中 ...

  2. 【京东详情页】——原生js爬坑之标签页

    一.引言 要做详情页的商品评价等5个li的标签页转换,效果如下: 二.实现原理 有一个特别的地方:上面五个li,但下面只有四个容器(table/div). 设计的目的:无论点哪个li,只有前四个div ...

  3. React获得真实的DOM操作

    真实的DOM操作 ------------------------------------------------------------------------------------------- ...

  4. GitHub使用(四) - 关于分支Branch

    1. 什么是分支Branch? 我初步的理解为:GitHub仓库默认有一个master的分支,当我们在master分支开发过程中接到一个新的功能需求,我们就可以新建一个分支同步开发而互不影响,开发完成 ...

  5. C++中const用于函数重载

    C++中const用于函数重载 常成员函数和非常成员函数之间的重载 首先先回忆一下常成员函数 声明:<类型标志符>函数名(参数表)const: 说明: (1)const是函数类型的一部分, ...

  6. Spark组件

    1,Application application(应用)其实就是用spark-submit提交的程序.比方说spark examples中的计算pi的SparkPi.一个application通常包 ...

  7. web.xml中servlet-mapping的配置

    <servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern>,& ...

  8. Android Studio 导入应用时报错 Error:java.lang.RuntimeException: Some file crunching failed, see logs for details

    在app文件夹的build.gradle里加上 android { ...... aaptOptions.cruncherEnabled = false aaptOptions.useNewCrunc ...

  9. Java字符串的匹配问题,String类的matches方法与Matcher类的matches方法的使用比较,Matcher类的matches()、find()和lookingAt()方法的使用比较

    参考网上相关blog,对Java字符串的匹配问题进行了简单的比较和总结,主要对String类的matches方法与Matcher类的matches方法进行了比较. 对Matcher类的matches( ...

  10. 基于java生成二维码

                                                                                            二维码  二维码的概念 ...