POJ2342:Anniversary party(树形dp)
来源:互联网 发布:科讯cms 编辑:程序博客网 时间:2024/05/16 04:57
Anniversary party
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7937 Accepted: 4549
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 N – 1 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
711111111 32 36 47 44 53 50 0
Sample Output
5
Source
Ural State University Internal Contest October'2000 Students Session
题意:一个公司有n个人,每个人有个活跃度,有n-1个上司与下属关系,现在举行一个party,要求下属与其直接上司不能同时出席,问怎样安排他们的出席使得party总活跃度最大。思路:大boss只有一个,从根节点开始dfs到叶子,从下属开始计算,dp[i][1]表示i出席party,以i为根的子树得到的最大活跃度,dp[i][0]表示i不出席。
①以边为主,速度较快。
# include <iostream># include <cstdio># include <vector># include <cstring># include <algorithm>using namespace std;int cnt=0, pre[6001], dp[6001][2], vis[6001];struct edge{ int s, e, last;}edge[6001];void Add_edge(int s, int e){ edge[cnt].s = s; edge[cnt].e = e; edge[cnt].last = pre[s];//记录以s为起点的上一条边的编号。 pre[s] = cnt++;}void dfs(int root){ if(pre[root] == -1) return; for(int i=pre[root]; i!=-1; i=edge[i].last) { int son = edge[i].e; dfs(son); dp[root][1] += dp[son][0]; dp[root][0] += max(dp[son][0], dp[son][1]); }}int main(){ int n, a, b, root; while(~scanf("%d",&n)) { memset(pre, -1, sizeof(pre)); memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; ++i) { scanf("%d",&dp[i][1]); dp[i][0] = 0; } while(scanf("%d%d",&a,&b),a+b) { Add_edge(b, a); vis[a]++; } for(int i=1; i<=n; ++i) if(!vis[i]) { root = i; break; } dfs(root); printf("%d\n",max(dp[root][1], dp[root][0])); } return 0;}
②以点为主,速度较慢,这里用邻接表表示该树。
# include <iostream># include <cstdio># include <cstring># include <vector># include <algorithm>using namespace std;int dp[6001][2], vis[6001];vector<int>v[6001];void dfs(int root){ for(int i=0; i<v[root].size(); ++i) { int son = v[root][i]; dfs(son); dp[root][1] += dp[son][0]; dp[root][0] += max(dp[son][0], dp[son][1]); }}int main(){ int n, a, b, root; while(~scanf("%d",&n)) { memset(vis, 0, sizeof(vis)); for(int i=1; i<=n; ++i) { scanf("%d",&dp[i][1]); dp[i][0] = 0; v[i].clear(); } while(scanf("%d%d",&a,&b),a+b) { v[b].push_back(a); vis[a]++; } for(int i=1; i<=n; ++i) if(!vis[i]) { root = i; break; } dfs(root); printf("%d\n",max(dp[root][0], dp[root][1])); } return 0;}
0 0
- poj2342 Anniversary party(树形dp)
- hdu1520 Anniversary party(poj2342,树形dp)
- POJ2342:Anniversary party(树形dp)
- poj2342 Anniversary party (树形Dp)
- POJ2342:Anniversary party(树形DP)
- poj2342 Anniversary party【树形dp】
- POJ2342 Anniversary party(树形DP)
- poj2342.Anniversary party(树形dp)
- POJ2342 Anniversary party 【树形dp】
- poj2342 Anniversary party (树形DP)
- POJ2342 Anniversary party(树形dp)
- poj2342——Anniversary party(树形dp)
- poj2342|hdu 1520 Anniversary party 树形dp
- hdu 1520 && poj2342 anniversary party树形DP
- Hdoj 1520&Poj2342 Anniversary party 【树形DP】
- POJ2342 Anniversary party 树形dp入门题
- URAL 1039 / poj2342-Anniversary Party-树形DP
- [树形dp][入门]hdu1520 & poj2342 Anniversary party
- Redis主从复制和集群配置系列之三
- JQuery基础一(样式篇)--内容选择器
- mysqldump 导出统一限制每张数据表导出的记录数
- 认认真真写android——Broadcast篇
- Android判断APP当前状态,处于前台还是后台或者未启动
- POJ2342:Anniversary party(树形dp)
- java单例设计模式
- graphviz中文支持和IBUS中文输入法支持
- MySQL性能调优——索引详解与索引的优化
- SLPA原理及详解
- 对输入框进行输入的时候有时候需要判断输入的是不是数字或者不只让他输入数字
- 让PHP更快的提供文件下载
- canvas五角星
- Android之程序崩溃后重启