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. 上传文件到服务器端后进一步推送到sftp服务器

    扩展安装 要想sftp服务端发送文件,就需要php脚本具有作为ssh客户端的能力,所以需先为php安装如下扩展 openssl openssl-dev libssh php ssh 扩展 按照下面的命 ...

  2. 怎样清除td和input之间空隙

    <style> input {background:red;border:none;height:30px;margin:0px} td {background-color:blue;pa ...

  3. Soy文件生成JS文件 - 一个使用Google soy模板的例子

    1.下载工具包,后解压. http://closure-templates.googlecode.com/files/closure-templates-for-javascript-latest.z ...

  4. JavaScript 排序算法——快速排序

    常见排序 javaScript 实现的常见排序算法有:冒泡排序.选择排序.插入排序.谢尔排序.快速排序(递归).快速排序(堆栈).归并排序.堆排序. 过程 "快速排序"的思想很简单 ...

  5. but has failed to stop it. This is very likely to create a memory leak(c3p0在Spring管理中,连接未关闭导致的内存溢出)

    以下是错误日志信息: 严重: The web application [/news] registered the JDBC driver [com.mysql.jdbc.Driver] but fa ...

  6. Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights

    一个简单的题: 感觉像计算几何,其实并用不到什么计算几何的知识: 方法: 首先对每条边判断一下,看他们能够把平面分成多少份: 然后用边来对点划分集合,首先初始化为一个集合: 最后如果点的集合等于平面的 ...

  7. Best Sequence

    poj1699:http://poj.org/problem?id=1699 题意:给你nge串,让你求出这些串组成的最小的串重叠部分只算一次. 题解:我的做法是DFS,因为数据范围只有10,就算是n ...

  8. Android 程序框架设计

    这篇文章主要内容来自于之前我讲的一个PPT文档,现在将其整理如下.欢迎指正.以下的内容都是来自于我自身的经验,欢迎大家多提自己的建议. 1.一些概念 模式的定义: 每个模式都描述了一个在我们的环境中不 ...

  9. 【Xamarin挖墙脚系列:Android最重要的命令工具ADB】

    原文:[Xamarin挖墙脚系列:Android最重要的命令工具ADB] adb工具提供了很好的基于命令的对系统的控制. 以前说过,安卓的本质是运行在Linux上的虚机系统.在Linux中,对系统进行 ...

  10. STL unordered_set

    http://www.cplusplus.com/reference/unordered_set/unordered_set/ template < class Key, // unordere ...