Anniversary Party

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 ;
}
上一篇:Bootstrap系列 -- 39. 导航条添加标题


下一篇:基于mindwave脑电波进行疲劳检测算法的设计(3)