hdoj 1520 Anniversary party 【树形DP入门】
来源:互联网 发布:越南废除户籍知乎 编辑:程序博客网 时间:2024/05/04 18:07
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5954 Accepted Submission(s): 2705
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
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
第一道树形dp:
#include<stdio.h>#include<string.h>#include<vector>#define MAX 6000+10using namespace std;int max(int x,int y){ return x>y?x:y; }int dp[MAX][2],vis[MAX];vector<int>tree[MAX];//存储子节点 void dfs(int node){ vis[node]=1; int i,j; int son;//子节点 for(i=0;i<tree[node].size();i++) { son=tree[node][i]; if(!vis[son]) { dfs(son); dp[node][1]+=dp[son][0];//上司去 下属不去 dp[node][0]+=max(dp[son][1],dp[son][0]); // 上司不去 下属去或不去 } }}int main(){ int n,i; int root;//根节点 int x,y; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%d",&dp[i][1]); dp[i][0]=0; vis[i]=1; tree[i].clear();//清空 } while(scanf("%d%d",&x,&y)&&(x!=0||y!=0)) { vis[x]=0;//x为下属 标记一下 tree[y].push_back(x);//建立关系 } root=0; for(i=1;i<=n;i++)//查询根节点 { if(vis[i]) { root=i;//记录根节点 break; } } memset(vis,0,sizeof(vis)); dfs(root); printf("%d\n",max(dp[root][0],dp[root][1])); } return 0;}
更新:2015.8.11
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define MAXN 6000+10using namespace std;vector<int> G[MAXN];int dp[MAXN][2];int pre[MAXN];int N;int find(int p){ int t; int child = p; while(child != p) p = pre[p]; while(child != p) { t = pre[child]; pre[child] = p; child = t; } return p;}void merge(int x, int y){ int fx = find(x); int fy = find(y); if(fx != fy) pre[fx] = fy;}void DFS(int u){ for(int i = 0; i < G[u].size(); i++) { int v = G[u][i]; DFS(v); dp[u][1] += dp[v][0]; dp[u][0] += max(dp[v][1], dp[v][0]); }}int main(){ while(scanf("%d", &N) != EOF) { int a, b, root; for(int i = 1; i <= N; i++) { scanf("%d", &a); dp[i][1] = a;//i去 dp[i][0] = 0;//i不去 pre[i] = i; G[i].clear(); } while(scanf("%d%d", &a, &b), a||b) G[b].push_back(a), pre[a] = b; for(int i = 1; i <= N; i++) { if(pre[i] == i) root = i; } DFS(root); printf("%d\n", max(dp[root][0], dp[root][1])); } return 0;}
0 0
- hdoj 1520 Anniversary party 【树形DP入门】
- hdoj 1520 Anniversary party(树形dp)
- Hdoj 1520&Poj2342 Anniversary party 【树形DP】
- HDU 1520 Anniversary party(树形DP入门)
- HDU 1520Anniversary party 树形DP入门
- HDU 1520 Anniversary party 树形DP入门
- hdu 1520 Anniversary party 树形dp入门
- HDU 1520 Anniversary party 树形dp 入门
- hdoj - 1520 - Anniversary party(基础树形DP)
- HDOJ 题目1520 Anniversary party(树形dp)
- HDU 1520 Anniversary party 树形DP入门题
- hdu 1520 Anniversary party(树形DP入门)
- HDU 1520 & POJ 2342 Anniversary party(树形DP入门题)
- HDU 1520 Anniversary party (简单树形DP入门)
- HDU-1520-Anniversary party(树形DP入门)
- Anniversary party(树形DP入门)
- POJ2342 Anniversary party 树形dp入门题
- [树形dp][入门]hdu1520 & poj2342 Anniversary party
- BZOJ 1878 [SDOI2009]HH的项链 离线+树状数组
- 判断程序是前台运行还是后台运行的方法?
- 剑指offer 面试题39—二叉树的深度vs平衡二叉树判断
- camera 应用
- hibernate学习-实体类与数据库表映射 分类: Hibernate 2012-11-01 17:21 541人阅读 评论(0) 收藏 举报 1.根据实体类名获取表名 Map<String, A
- hdoj 1520 Anniversary party 【树形DP入门】
- Ubuntu安装 配置 vsftpd
- FFT倒序算法——Rader算法
- 解决问题方式的转变3
- 适配遇到的一些问题
- Java项目:Eclipse导出终端(命令行)可运行的jar文件
- v4l2 编程接口(一) — ioctl
- appStore 应用审核-加急审核
- Elevator(电梯)