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. 关于ios::sync_with_stdio(false)

    作用就是取消同步,这样的话使用cin就和使用scanf效率相似. 但是今天在做题的时候碰到一点小问题,就是在关闭同步的时候使用scanf是交了一发代码,然后RE了(经检查scanf没有写错),而把关同 ...

  2. 初入ubuntu

    登入root :su root 安装 vim: sudo apt-get install vim 安装 gcc(g++):sudo apt-get install gcc(g++) 非常实用的修改分辨 ...

  3. XML的序列化(Serializer)

    步骤: //1获取XmlSerializer 类的实例 通过Xml这个工具类去获取 XmlSerializer xmlSerializer = Xml.newSerializer(); try { / ...

  4. python 库之lxml安装 坑一个

    error: command 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python ...

  5. 600集Python从入门到精通教程(懂中文就能学会)

    目录大纲: 本套教程15天 1-3   天内容为Linux基础命令 4-13  天内容为Python基础教程 14-15 天内容为 飞机大战项目演练 视频概括: 第一阶段(1-3天): 该阶段首先通过 ...

  6. 你不容错过的 腾讯 AlloyTeam Web 前端大会 看点完全剖析

    AC大会 ( Alloyteam Conf ),是由腾讯前端技术团队的标杆团队 AlloyTeam 发起的前端技术大会,旨在分享团队在技术研究.产品研发.开源项目的经验沉淀.AC2017 将会继续在工 ...

  7. Java数据结构和算法总结-数组、二分查找

    前言:在平时开发中数组几乎是最基本也是最常用的数据类型,相比链表.二叉树等又简单很多,所以在学习数据和算法时用数组来作为一个起点再合适不过了.本篇博文的所有代码已上传 github ,对应工程的 ar ...

  8. 51nod 1451 合法三角形 判斜率去重,时间复杂度O(n^2)

    题目: 这题我WA了3次,那3次是用向量求角度去重算的,不知道错在哪了,不得不换思路. 第4次用斜率去重一次就过了. 注意:n定义成long long,不然求C(3,n)时会溢出. 代码: #incl ...

  9. NOIP2017SummerTraining0720

    这套题,看到第一题就想到了以前做过的运动鞋那道题,然后就往DP这个方向去思考,想来想去每什么思路,然后就去看第二题,第二题一看就是一道最短路的问题,但是它的建图十分困难, 怎么打都不知到该怎么建图,然 ...

  10. MYSQL解压版安装说明

    一. zip格式,解压缩之后要进行配置.解压之后可以将该文件夹改名,放到合适的位置,比如把文件夹改名为MySQL(文件夹 MySQL下面就是 bin, data,my-default.ini 等) 二 ...