hdu oj 1520 Anniversary party(树形dp入门)
2015-09-01 12:32
417 查看
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6990 Accepted Submission(s): 3104
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
Output should contain the maximal sum of guests' ratings.
[align=left]Sample Input[/align]
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0
[align=left]Sample Output[/align]
5
[align=left]Source[/align]
Ural State University Internal Contest October'2000 Students Session
题意:n个人(编号1-N),然后n行分表代表第n个人的活跃度,之后若干行 L 和 K(0 0结束),代表K是L的上司,
问一个聚会中邀请这n个人中的若干,其中不能含直接的上下级关系,可以使聚会中的活跃度最大为多少。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <vector> #define ll long long using namespace std; const int N = 10000; int dp [3],root ; vector<int>G ; void DFS(int cur) { for(int i = 0; i < (int)G[cur].size(); i++) { int to = G[cur][i]; DFS(to); dp[cur][1] += dp[to][0];//该级去,下一级不去 dp[cur][0] += max(dp[to][0],dp[to][1]);//该级不去,下级去不去选最优 } } int main(void) { int n; while(scanf("%d",&n) != EOF) { memset(dp,0,sizeof(dp)); for(int i = 1; i <= n; i++) { scanf("%d",&dp[i][1]);//代表第几个人去能增加的活跃度 root[i] = i;//并查集的初始化 G[i].clear(); } int a,b; while(scanf("%d %d",&a,&b),a != 0 ||b != 0) { G[b].push_back(a);//vector建图 root[a] = b; } int beg; for(int i = 1; i <= n; i++) { if(root[i] == i)//查找根节点 { beg = i; break; } } DFS(beg); printf("%d\n",max(dp[beg][0],dp[beg][1])); } return 0; }
相关文章推荐
- mysql-安装之cmake的编译安装(mysql 5.6以后需要cmake编译安装)
- swift总结1
- mysql-安装之CAMKE编译安装
- 常用正则表达式
- mac ngrok使用
- 解决eclipse+MAVEN提示One or more constraints have not been satisfied.的问题
- 【转载】我的Lean & Agile(精简和敏捷)经历
- Debian root用户无法远程登录
- CUDA入门的学习资料
- JNI编程指南-第十一章 JNI设计思想概述
- php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法
- mysql utc 时间格式化
- Fiddler初步---认识Fiddler
- js实现具有高亮显示效果的多级菜单代码
- NYOJ 116 士兵杀敌(二)(线段树区间求和+单点更新)
- 自动任务中时间拼法
- JNI编程指南-第十章 使用JNI时容易出错的地方
- GIT push 遇到 access denied问题
- 数据结构学习之路-第二章:带头结点的线性链表
- PHP 数组转字符串后仍保留数组格式