Anniversary party
来源:互联网 发布:java服务器空间 编辑:程序博客网 时间:2024/04/30 04:36
Problem DescriptionThere 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.InputEmployees 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 0OutputOutput should contain the maximal sum of guests' ratings.Sample Input711111111 32 36 47 44 53 50 0Sample Output5//题意:给出N个点,构成一棵树,每个点都有权值,求去没有直接亲属关系的点的权值,使它们的权值最大。//题解:树状DP,状态转移方程:dp[i][0] = max(dp[j][0],dp[j][1]), dp[i][1] += dp[j][0]; (j是i的子节点)dp[i][0]表示不去i点能够得到最大值,dp[i][1]表示取i点能够等到最大值。//标程:#include<iostream>#include<cstdio>#include<vector>#include<cstring>using namespace std;vector<int> vec[6001];int dp[6005][2],vis[6001];void dfs(int x){ for(int i = 0; i < vec[x].size(); ++ i) { int y = vec[x][i]; dfs(y); dp[x][1] += dp[y][0]; dp[x][0] += max(dp[y][0],dp[y][1]); }}int main(){// freopen("a.txt","r",stdin); int n, i, j, a, b; while(scanf("%d",&n)!=EOF) { memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); for(i = 1; i <= n; ++ i) vec[i].clear(); for(i = 1; i <= n; ++ i) scanf("%d",&dp[i][1]); while(scanf("%d%d",&a,&b)!=EOF) { if(a+b == 0) break; vec[b].push_back(a); vis[a] = 1; } for(i = 1; i <= n; ++ i) if(vis[i] == 0) { dfs(i); break; } cout << max(dp[i][0],dp[i][1]) << endl; } return 0;}
0 0
- Anniversary party
- Anniversary Party
- Anniversary party
- Anniversary party
- Anniversary party
- ural 1039 Anniversary Party
- HDU 1520 Anniversary party
- HDU1520 Anniversary party
- ural 1039 Anniversary Party
- POJ2342 HDU1520 Anniversary party
- 【树形DP】Anniversary party
- hdoj 1520 Anniversary Party
- poj2342 Anniversary party
- poj 2342 Anniversary party
- HDU-1520-Anniversary party
- HDU-1520-Anniversary party
- HDU 1520(Anniversary party)
- POJ 2342 Anniversary party
- 弱事件管理器及WeakEventManager,IWeakEventListener的使用
- 杭电 1395
- ppt转pdf软件免费下载哪个好
- org.apache.commons.collections.CollectionUtils
- 拓扑排序(HDU 1285、HDU 5695)
- Anniversary party
- xcode 内存泄露
- 百度员工离职总结:如何做个好员工
- Java设计模式--代理模式
- Linux设备驱动程序简介
- 经纬财富:济宁黄金白银操作思路更新
- 无法通过windows installer服务安装此安装程序包。您必须安装带有更新版本windows Installer服务的Windows
- C++编译器
- springMvc 文件上传