hdu 1520Anniversary party(树形dp)
来源:互联网 发布:网络受限怎么办win8 编辑:程序博客网 时间:2024/09/21 09:04
<看完之后感觉要死的算法系列>
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7539 Accepted Submission(s): 3310
Problem 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
思路:
一些大牛说,图论的题难就难在建图,唉。
用dp[i][0]和dp[i][1]分别表示第i个人不参加与参加。
易得动归方程为:dp[i][1]=dp[j][0];
dp[i][0]=max(dp[j][0],dp[j][1]);
其中i和j是上司和下属的关系,若i参加,j不可以参加;若i不参加,j随意。
#include <limits.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <algorithm>#include <iostream>#include <iterator>#include <sstream>#include <queue>#include <stack>#include <string>#include <vector>#include <set>#include <map>//#define ONLINE_JUDGE#define eps 1e-6#define INF 0x7fffffff //INT_MAX#define inf 0x3f3f3f3f //int??????????????????#define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a);#define MEM(a) (memset((a),0,sizeof(a)))#define sfs(a) scanf("%s",a)#define sf(a) scanf("%d",&a)#define sfI(a) scanf("%I64d",&a)#define pf(a) printf("%d\n",a)#define pfI(a) printf("%I64d\n",a)#define pfs(a) printf("%s\n",a)#define sfd(a,b) scanf("%d%d",&a,&b)#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)#define for1(i,a,b) for(int i=(a);i<b;i++)#define for2(i,a,b) for(int i=(a);i<=b;i++)#define for3(i,a,b)for(int i=(b);i>=a;i--)#define MEM1(a) memset(a,0,sizeof(a))#define MEM2(a) memset(a,-1,sizeof(a))#define MEM3(a) memset(a,0x3f,sizeof(a))#define LL __int64const double PI = acos(-1.0);template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }using namespace std;template<class T>T Mint(T a, T b, T c) { if (a>b) { if (c>b) return b; return c; } if (c>a) return a; return c;}template<class T>T Maxt(T a, T b, T c) { if (a>b) { if (c>a) return c; return a; } else if (c > b) return c; return b;}const int maxn=6005;int T,n,m;int degree[maxn],val[maxn],head[maxn],vis[maxn],dp[maxn][2];int sum,tot,a,b;//链式前向星方式建图struct node{ int from,to; int next;}edge[maxn<<1];void addEdge(int a,int b){ edge[tot].from=a,edge[tot].to=b; edge[tot].next=head[a]; head[a]=tot++;}void dfs(int root){//递归查找该点的贡献度 int ans0,ans1; ans0=ans1=0; vis[root]=0; for(int j=head[root];j!=-1;j=edge[j].next){ int u=edge[j].to; if(!vis[u]) dfs(u); ans0+=max(dp[u][0],dp[u][1]); ans1+=dp[u][0]; } dp[root][0]=ans0; dp[root][1]=ans1+val[root];}int main() { while(~sf(n)){ MEM1(dp); MEM1(vis);//记录该节点是否已访问 MEM2(head); MEM1(degree);//记录节点的入度 sum=tot=0; for2(i,1,n) sf(val[i]);//记录第i个节点的贡献度 while(sfd(a,b)){ if(!a&&!b) break; addEdge(b,a); degree[a]++; } for2(i,1,n){ if(!degree[i]){ dfs(i); sum+=max(dp[i][0],dp[i][1]); } } pf(sum); } return 0;}
- 树形DP---hdu 1520 Anniversary Party
- HDU 1520 Anniversary party(树形DP入门)
- HDU 1520 Anniversary party-----树形DP
- hdu(1520) Anniversary party(树形dp)
- 【树形DP】 HDU 1520 Anniversary party
- HDU 1520 Anniversary party(简单树形dp)
- HDU 1520 Anniversary party 树形DP
- HDU 1520 Anniversary party (树形DP)
- hdu 1520 Anniversary party(树形dp)
- HDU 1520 Anniversary party(树形DP)
- hdu 1520 Anniversary party(基本树形DP)
- HDU 1520 Anniversary party (树形dp)
- HDU 1520 Anniversary party / 树形DP水题!!!
- hdu 1520 Anniversary party(树形dp)
- hdu 1520Anniversary party(简单树形dp)
- 【树形DP】hdu 1520 Anniversary party
- HDU 1520 - Anniversary party (树形DP)
- poj2342|hdu 1520 Anniversary party 树形dp
- mysqld与mysqld_safe的区别
- enum类型
- 扣丁学堂——SharedPreferences
- Unix Network Programming(12)---listen 函数
- 使用文件进行数据存储
- hdu 1520Anniversary party(树形dp)
- c# 动态操作 zip\rar 文件,压缩解压组件使用
- leetcode -- Path Sum -- 重点
- 让Ubuntu的vi支持backspace
- 实验七(以邻接矩阵为储存方式的广度优先搜索和以邻接表为储存方式的深度优先搜索方式)
- dw1000 PA和LNA控制
- 第一个struts2项目
- 获取main方法的返回值
- jquery记录