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. wdcp日志

    apache或nginx都有开关默认日志,一个是正常访问日志,一个是错误的日志,目录在 /www/wdlinux/nginx-1.0.15/logs /www/wdlinux/httpd-2.2.22 ...

  2. Delphi接口

    program Demo1; { Create Date: 2014-06-29 Author: P.S.M 1.接口Demo1 } {$APPTYPE CONSOLE} uses SysUtils; ...

  3. 如何在windows上安装部署设置SVN服务器

    1   一.准备工作 1.SVN服务器:解压缩包,可以从官方网站下载最新版本. 2.SVN客户端:TortoiseSVN,即常说的小乌龟,是一个客户端程序,用来与服务器端通讯. 2 二.安装服务器和客 ...

  4. Poco版信号槽

    #include "Poco/BasicEvent.h"#include "Poco/Delegate.h"#include <iostream> ...

  5. Keil的c语言编译器

    我曾经通过查看反汇编代码对KEILC编译器进行了测试,大概有这么一下内容,也得出一些结论. (1)全局变量:如果程序中定义了全局变量,而且初始值不是0.此时,在程序调到main()函数执行前,除了要进 ...

  6. 简单的网页采集程序(ASP.NET MVC4)

    因为懒人太多,造成现在网页数据采集非常的流行,我也来写个简单的记录一下. 之前写了MVC的基本框架的搭建随笔,后面因为公司太忙,个人感情问题:(,导致不想写了,就写了两篇给删除了,现在就搁浅了, 本人 ...

  7. shutdown computer in ad and ou

    1. powershell Shutdown-computer –computername (gc c:\temp\serverlist.txt) –force –throttlelimit 10 h ...

  8. iOS 静态库中使用宏定义区分iPhone模拟器与真机---备用

    问题描述 一般项目中,可以使用宏定义来判断模拟器还是真机,这无疑是有效的. #if TARGET_IPHONE_SIMULATOR #define SIMULATOR 1 #elif TARGET_O ...

  9. MyEclipse过期激活方法

    如果已经过期会提示,进行购买.重新激活和退出,我们选择重新激活. 打开浏览器,地址栏输入key.858game.com,然后输入名称,在线获得MyEclipse的激活码. 输入Sumscripter: ...

  10. JNI/NDK开发指南(一)—— JNI开发流程及HelloWorld

    转载请注明出处:http://blog.csdn.net/xyang81/article/details/41777471 JNI全称是Java Native Interface(Java本地接口)单 ...