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
Sample Output
5
此题同poj为一题,但是同样的代码pojAC,hdu却T了,题意就是聚会保证活跃度最大,但是限制具有上下等级关系的不能在一起,,,
开始用poj的代码一直T,后来改为邻接表93ms~~~~~~
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~~~~~~
5721163 | qwerqqq![]() | A![]() | Accepted | 1760 | 93 | G++ | 1726 | 24 hr ago |
5721115 | qwerqqq![]() | A![]() | Time Limit Exceeded | G++ | 1419 | 24 hr ago | ||
5644872 | qwerqqq![]() | A![]() | Wrong Answer | G++ | 1332 | 11 days ago | ||
5644870 | qwerqqq![]() | A![]() | Wrong Answer | G++ | 1334 | 11 days ago | ||
5644868 | qwerqqq![]() | A![]() | Time Limit Exceeded | G++ | 1330 | 11 days ago | ||
5644864 | qwerqqq![]() | A![]() | Time Limit Exceeded | G++ | 1328 | 11 days ago | ||
5644862 | qwerqqq![]() | A![]() | Time Limit Exceeded | C++ | 1328 | 11 days ago | ||
5644816 | qwerqqq![]() | A![]() | Time Limit Exceeded | C++ | 1417 | 11 days ago | ||
5644809 | qwerqqq![]() | A![]() | Time Limit Exceeded | G++ | 1417 | 11 days ago | ||
5644804 | qwerqqq![]() | A![]() | Time Limit Exceeded | G++ | 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; } |
相关文章推荐
- 【bzoj3282】【Tree】【lct】
- position定位
- position定位
- Boost编译和配置教程(Win10+VS2013+Boost1.60.0)
- POJ 1064 Cable master(二分查找 求近似解)
- YII2.0 验证表单
- Sentos设置开机启动服务
- Uva 10635 简单dp
- 从AlphaGo谈通用型人工智能设计
- 经典排序之 选择排序
- Nyoj 天下第一(spfa)
- MYSQL ERROR CODE 错误编号的意义
- 1.7-Redis介绍和安装
- 仿微信语音功能的流程
- dfsfa
- Node.js基础学习之常见API理解
- 二叉树查找、匹配字符串、快速排序
- 刘钦哥哥的金句
- hdu_2668 Daydream O(n)求最长不重复子串
- 如何配置Apache虚拟主机?(基于IP、基于端口、基于域名)