树形dp--Anniversary party
来源:互联网 发布:论坛源码php源码 编辑:程序博客网 时间:2024/05/17 03:12
Anniversary party
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12972 Accepted Submission(s): 5203
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
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
Source
Ural State University Internal Contest October’2000 Students Session
题意:
给你一个只有一个根节点数,每个节点都有一个值,限制就是所选取的节点不能有相邻着的,问,所选节点的最大和是多少
。。第一次做树形dp的题目,感觉就是数塔,多了个限制条件而已
思路:
遍历到叶子节点,然后往父节点回溯,
#include <iostream>#include <string.h>#include<vector>using namespace std;int vis[6005];int dp[6005][2];int isroot[6005];vector<int > T[6005];void Dfs(int x){ for(int i=0; i<T[x].size(); ++i) { int t = T[x][i]; if(!vis[t]) Dfs(t); } for(int i=0; i<T[x].size(); ++i) { int t = T[x][i]; dp[x][1] += dp[t][0]; dp[x][0] += max(dp[t][0],dp[t][1]); } }int main(void){// freopen("in.txt","r",stdin); ios::sync_with_stdio(false); int n,v,u; while(cin >> n) { int root=1; memset(isroot,0,sizeof(isroot)); memset(vis,0,sizeof(vis)); memset(dp,0,sizeof(dp)); for(int i=1; i<=n; ++i) { cin>>dp[i][1]; T[i].clear(); } while(cin>>u>>v) { if(!u && !v) break; T[v].push_back(u); isroot[u] = 1; } for(int i=1; i<=n; ++i) { if(!isroot[i]) { root = i; break; } } Dfs(root); cout << max(dp[root][0],dp[root][1])<<endl; } return 0;}
- 【树形DP】Anniversary party
- Anniversary party--树形DP
- Anniversary Party(树形dp)
- 树形dp--Anniversary party
- 树形DP- Anniversary party
- hdu1520 Anniversary party 树形dp
- POJ2342:Anniversary party(树形DP)
- HDU1520:Anniversary party(树形DP)
- Anniversary party(树形dp)
- hdu1520 Anniversary party(树形DP)
- hdu1520 Anniversary party (树形dp)
- poj2342 Anniversary party【树形dp】
- Anniversary party(树形DP)
- poj Anniversary party (树形DP)
- POJ2342 Anniversary party(树形DP)
- poj2342.Anniversary party(树形dp)
- hdu1520 Anniversary party 【树形dp】
- HDU1520 Anniversary party(树形DP)
- 并不是所有的标签都会提交到服务器
- c语言操作符总结
- 矩阵求导方法
- KafkaOffsetMonitor:监控消费者和延迟的队列
- 每日一记
- 树形dp--Anniversary party
- 密码学1
- java中instanceof的用法
- RMI 说明
- 练习 2-4 squeeze(s1, s2),将字符串 s1 中任何与字符串 s2 中字符匹配的字符 都删除
- 例2.2
- String、StringBuffer与StringBuilder之间区别
- 计网TCP连接中的3次握手
- 今日收获:匿名函数 Lambda