Time limit: 0.5 second

Memory limit: 8 MB

Background

The president of the Ural State University is going to make an 80'th Anniversary party. The university has a hierarchical structure of employees; that is, the supervisor relation forms a tree rooted at the president. Employees are numbered by integer numbers in a range from 1 to N, The personnel office has ranked each employee with a conviviality rating. In order to make the party fun for all attendees, the president does not want both an employee and his or her immediate supervisor to attend.

Problem

Your task is to make up a guest list with the maximal conviviality rating of the guests.

Input

The first line of the input contains a number N. 1 ≤ N ≤ 6000.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 the supervisor relation tree goes.Each line of the tree specification has the formwhich means that the K-th employee is an immediate supervisor of L-th employee. Input is ended with the line0 0

Output

The output should contain the maximal total rating of the guests.

Sample

input output
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
5

算是比较基础的树形dp的题吧 ,qwq  ,先要寻找根节点 。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std ;
const int inf = << , maxn = + ;
int n ,fen[maxn] , f[maxn][] , head[maxn] , cnt , ru[maxn] ;
struct id
{
int nxt , to ;
} edge[maxn] ; void add( int u , int v )
{
edge[++cnt].to = v , edge[cnt].nxt = head[u] ;
head[u] = cnt ;
} void Init( )
{
scanf( "%d" , &n ) ; int l ,k ;
for( int x = ; x <= n ; ++x ) scanf( "%d" , fen+x ) ;
while( )
{
scanf( "%d%d" , &l , &k ) ;
if( l == k && k == ) break ;
add( k , l ) ;
ru[l]++ ;
}
} int dfs( int u , int use )
{
if( ~f[u][use] ) return f[u][use] ;
int v = ; f[u][use] = ;
for( int x = head[u] ; x ; x = edge[x].nxt )
{
v = edge[x].to ;
if( use == ) f[u][use] += dfs( v , ) ;
else
{
f[u][use] += max( dfs(v , ) , dfs(v , ) ) ;
}
}
if( use == ) f[u][use] += fen[u] ;
return f[u][use] ;
} void Solve( )
{
int ans = ;memset( f , - , sizeof(f) ) ;
for( int x = ; x <= n ; ++x )
{
if( !ru[x] )
{
// cout<<x<<endl;
ans = max( ans , max( dfs( x , ) , dfs( x , ) ) ) ;
}
}
printf( "%d\n" , ans ) ;
} int main( )
{
Init( ) ;
Solve( ) ;
return ;
}

Anniversary Party的更多相关文章

  1. (UWP开发)基于Windows10 Anniversary SDK创造出位于可视化层的DropShadow

    Windows.UI.Composition API是可以从任何通用Windows平台应用程序调用的声明性保留模式API,从而可以直接在应用程序中创建合成对象.动画和效果. Composition A ...

  2. POJ 2342 Anniversary party(树形dp)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7230   Accepted: 4162 ...

  3. 微软四十周年 Microsoft’s 40th anniversary

    比尔-盖茨在4月3日给微软全体员工写了这封邮件,原文是英文,我们翻译了中文.图片是后加上的. 明天将是特殊的一天:微软的40周年纪念日. Tomorrow is a special day: Micr ...

  4. HDOJ 1520 Anniversary party

    树形DP....在树上做DP....不应该是猴子干的事吗?  Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  5. hdu1520 树形dp Anniversary party

    A - Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  6. hdu 1520 Anniversary party 基础树dp

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

  7. 【Poj】 p2342 Anniversary party(树形DP第一道)

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

  8. poj 2342 Anniversary party 简单树形dp

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3862   Accepted: 2171 ...

  9. BestCoder 1st Anniversary B.Hidden String DFS

    B. Hidden String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/co ...

  10. HDU 1520:Anniversary party(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description   There i ...

随机推荐

  1. PHP设计模式之工厂/单例/注册者模式

    工厂模式 简单工厂模式 [静态工厂方法模式](Static Factory Method)是类的创建模式 工厂模式的几种形态: 1.简单工厂模式(Simple Factory)又叫做 静态工厂方法模式 ...

  2. php学习,一个简单的Calendar(2) 一个简单的活动页面

    有了前面的基础,后面就是将页面展示出来. 预览图如下:1号和31号分别有活动,会一并显示出来   这里需要搞定几个问题,一个就是数据库的连接,我们用\sys\class\class.db_connec ...

  3. C# MySql 操作类

    /* MySql 类 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  4. Ubuntu的关机重启命令知识

    Ubuntu的关机重启命令知识,以作备忘. 重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(roo ...

  5. LFS实践

    用了三天,编译了两次LFS,把LFS的基本流程和原理都弄清了.用的是LFS 6.3,使用的教程是LFS速成手册(6.3) ,感觉很不错,如果按照它的做法,一步一步来,基本都能编译成功而且没什么错误.不 ...

  6. PHP下拉框选择的实现方法

    实现 第一种PHP下拉框实现方法: < ?php //提交下拉框; //直接饱触发onchange事件的结果 $id=$_GET['myselect']; // myselect 为locati ...

  7. 【原】jQuery编写插件

    分享一下编写设置和获取颜色的插件,首先我将插件的名字命名为jquery.color.js.该插件用来实现以下两个功能1.设置元素的颜色.2.获取元素的颜色. 先在搭建好如下编写插件的框架: ;(fun ...

  8. PHP之路——大文件上传

    修改php.ini 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本最大执行时间,超过这个时间就会报错 3.upl ...

  9. (转) 各种好用的插件 Xcode

    时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我提升编码速度和工作效率的工具 ...

  10. 支付宝Demo 报错

    支付宝SDK-------DEMO第一次编译肯定是会报错的: 修正的方法为:   打开项目属性->Build Settings 找到 Library SearchPaths   看见里面的参数了 ...