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. Thinkphp5.0 在自己定义一个公共方法的控制器并且继承了Controller类的时候报错

    在建立网站的时候,你通常想着把一些共有的方法提取出来,放入一个控制器内,如果你是将业务逻辑写入了构造函数里面,那么就得注意了. 在thinkphp5.0当中,有一个初始化的方法,类似于构造函数,那就是 ...

  2. ioc(Inversion of Control)控制反转和DI

    ioc意味着将你设计好的交给容器控制,而不是传统在你的对象中直接控制 谁控制了谁:传统的javaSE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象:而ioc是有专门一个容 ...

  3. Web 开发模式演变历史和趋势

    前不久徐飞写了一篇很好的文章:Web 应用的组件化开发.本文尝试从历史发展角度,说说各种研发模式的优劣. 一.简单明快的早期时代 可称之为 Web 1.0 时代,非常适合创业型小项目,不分前后端,经常 ...

  4. HashMap的源码分析(一)

    1.hashMap的关键值 DEFAULT_INITIAL_CAPACITY:默认初始容量16,∈(0,1<<30),实际大小为2的整数次幂: DEFAULT_LOAD_FACTOR:默认 ...

  5. 深入浅出数据结构C语言版(22)——排序决策树与桶式排序

    在(17)中我们对排序算法进行了简单的分析,并得出了两个结论: 1.只进行相邻元素交换的排序算法时间复杂度为O(N2) 2.要想时间复杂度低于O(N2),算法必须进行远距离的元素交换 而今天,我们将对 ...

  6. VC维含义的个人理解

    有关于VC维可以在很多机器学习的理论中见到,它是一个重要的概念.在读<神经网络原理>的时候对一个实例不是很明白,通过这段时间观看斯坦福的机器学习公开课及相关补充材料,又参考了一些网络上的资 ...

  7. 关于Class对象、类加载机制、虚拟机运行时的内存布局的全面解析和推测

    简介: 本文是对Java的类加载机制,Class对象,反射原理等相关概念的理解.验证和Java虚拟机中内存布局的一些推测.本文重点讲述了如何理解Class对象以及Class对象的作用. 欢迎探讨,如有 ...

  8. Python实战之实现简单的购物车系统

    #!usr/bin/env Python3 # -*-coding:utf-8-*- # 程序:购物车程序 # # 需求: # # 启动程序后,让用户输入工资,然后打印商品列表 # 允许用户根据商品编 ...

  9. zabbix基本操作

    zabbix基本操作 ---- 2016年终总结 二 包括的内容: 添加主机 查看监控数据 添加监控项 创建触发器 创建模版 添加报警 添加媒介 添加主机 进入页面 点击Configuration(大 ...

  10. fatal: The remote end hung up unexpectedly

    git push 的时候出错,提示: fatal: The remote end hung up unexpectedly 遇见几次了,原因是因为文件太大,把限制放宽就好了.命令: git confi ...