hdu1520 树形dp Anniversary party
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
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~~~~~~
5721163 | qwerqqq![]() |
A![]() |
Accepted
|
1760 | 93 | 1726 |
24 hr ago
|
|
5721115 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1419 |
24 hr ago
|
|||
5644872 | qwerqqq![]() |
A![]() |
Wrong Answer
|
1332 |
11 days ago
|
|||
5644870 | qwerqqq![]() |
A![]() |
Wrong Answer
|
1334 |
11 days ago
|
|||
5644868 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1330 |
11 days ago
|
|||
5644864 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1328 |
11 days ago
|
|||
5644862 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1328 |
11 days ago
|
|||
5644816 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1417 |
11 days ago
|
|||
5644809 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1417 |
11 days ago
|
|||
5644804 | qwerqqq![]() |
A![]() |
Time Limit Exceeded
|
1354 |
11 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; }
|