您的位置:首页 > 其它

hdu1520 树形dp Anniversary party

2016-03-24 21:03 155 查看
A - Anniversary party
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1520

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 T 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
此题同poj为一题,但是同样的代码pojAC,hdu却T了,题意就是聚会保证活跃度最大,但是限制具有上下等级关系的不能在一起,,,
开始用poj的代码一直T,后来改为邻接表93ms~~~~~~
5721163qwerqqq

A

Accepted
176093G++
172624 hr ago
5721115qwerqqq

A

Time Limit Exceeded
G++
141924 hr ago
5644872qwerqqq

A

Wrong Answer
G++
133211 days ago
5644870qwerqqq

A

Wrong Answer
G++
133411 days ago
5644868qwerqqq

A

Time Limit Exceeded
G++
133011 days ago
5644864qwerqqq

A

Time Limit Exceeded
G++
132811 days ago
5644862qwerqqq

A

Time Limit Exceeded
C++
132811 days ago
5644816qwerqqq

A

Time Limit Exceeded
C++
141711 days ago
5644809qwerqqq

A

Time Limit Exceeded
G++
141711 days ago
5644804qwerqqq

A

Time Limit Exceeded
G++
135411 days ago

#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>

using namespace std;

#define maxn 6005
struct node{
int to,net;
}que[maxn<<1];
int head[maxn];
int n;
int dp[maxn][2],father[maxn];//dp[i][0]0表示不去,dp[i][1]1表示去了
bool visited[maxn];
int tot=0;

void addedge(int u,int v){
que[tot].to=v;
que[tot].net=head[u];
head[u]=tot++;

que[tot].to=u;
que[tot].net=head[v];
head[v]=tot++;

}

void tree_dp(int node)
{
int i;
visited[node] = 1;
for(i=head[node]; i!=-1; i=que[i].net)
{
int v=que[i].to;
if(!visited[v]&&father[v] == node)//i为下属
{
tree_dp(v);//递归调用孩子结点,从叶子结点开始dp
//关键
dp[node][1] += dp[v][0];//上司来,下属不来
dp[node][0] +=max(dp[v][1],dp[v][0]);//上司不来,下属来、不来
}
}
}

int main()
{
int i;
int f,c,root;
while(scanf("%d",&n)!=EOF)
{
tot=0;
memset(head,-1,sizeof(head));
memset(dp,0,sizeof(dp));
memset(father,0,sizeof(father));
memset(visited,0,sizeof(visited));
for(i=1; i<=n; i++)
{
scanf("%d",&dp[i][1]);
}
root = 0;//记录父结点
bool beg = 1;
while (scanf("%d %d",&c,&f),c||f)
{
addedge(c,f);
father[c] = f;
if( root == c || beg )
{
root = f;
}
}
while(father[root])//查找父结点
root=father[root];
tree_dp(root);
int imax=max(dp[root][0],dp[root][1]);
printf("%d\n",imax);
}
return 0;

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: