POJ 2486 Apple Tree ( 树型DP )
来源:互联网 发布:商家优化方案 编辑:程序博客网 时间:2024/05/23 01:04
#include <iostream>#include <cstring>#include <deque>using namespace std;#define SIZE 230#define BACK 1#define AWAY 0int DP[SIZE][SIZE][2];bool visits[SIZE];int vals[SIZE];deque< int > tree[SIZE];int num, steps;void dfs( int u ){ visits[u] = true; const int len = tree[u].size(); for( int i = 0; i <= steps; ++i ) DP[u][i][BACK] = DP[u][i][AWAY] = vals[u]; for( int i = 0; i < len; ++i ){ int son = tree[u][i]; if( visits[son] ) continue; dfs( son ); for( int s = steps; s >= 0; --s ){ for( int ss = 0; ss <= s; ++ss ){ /* 从 u 出发,回到 u,需要多走两步 u->son,son->u, 分配给 son 子树 ss 步,其他子树 s - ss 步,都返回. */ DP[u][s + 2][BACK] = max( DP[u][s + 2][BACK], DP[u][s - ss][BACK] + DP[son][ss][BACK] ); /* 不回 u (去 u 的其他子树),在 son 返回. */ DP[u][s + 2][AWAY] = max( DP[u][s + 2][AWAY], DP[u][s - ss][AWAY] + DP[son][ss][BACK] ); /* 先遍历 u 的其他子树,回到 u 后,遍历 son 子树, 在当前子树 son 不返回,多走一步. */ DP[u][s + 1][AWAY] = max( DP[u][s + 1][AWAY], DP[u][s - ss][BACK] + DP[son][ss][AWAY] ); } } }}int main(){ int u, v; while( cin >> num >> steps ){ memset( DP, 0, sizeof( DP ) ); memset( visits, false, sizeof( visits ) ); for( int i = 1; i <= num; ++i ) tree[i].clear(); for( int i = 1; i <= num; ++i ) cin >> vals[i]; for( int i = 1; i <= num - 1; ++i ){ cin >> u >> v; tree[u].push_back( v ); tree[v].push_back( u ); } dfs( 1 ); cout << max( DP[1][steps][BACK], DP[1][steps][AWAY] ) << endl; } return 0;}
1 0
- POJ 2486 Apple Tree(树型DP)
- POJ 2486 Apple Tree ( 树型DP )
- 【POJ 2486】 Apple Tree(树型dp)
- POJ 2486 Apple Tree 树形DP
- POJ 2486 Apple Tree 树形dp
- POJ--2486--Apple Tree--树形回溯DP
- 【树形DP】 POJ 2486 Apple Tree
- poj 2486 Apple Tree(经典树形DP)
- poj 2486 Apple Tree 树形dp
- *(中等) 树形dp POJ 2486 Apple Tree
- poj 2486 Apple Tree (树形dp)
- poj 2486 Apple Tree(树形dp)
- POJ 2486 Apple Tree(树形dp)
- Apple Tree - POJ 2486 树形dp
- POJ 2486 Apple Tree 树形DP
- poj 2486 Apple Tree (树形dp)
- poj 2486 Apple Tree(树形dp)
- POJ 2486 Apple Tree (树形dp)
- html语言用重叠的方式做可输入的下拉框
- 4分44秒的演讲,却让整个世界都沉默了
- UCGUI响应物理按键的思路,没有用触摸
- Linux - 打印目录所有文件 代码(C)
- Python基础01 Hello World!
- POJ 2486 Apple Tree ( 树型DP )
- MySQL索引背后的数据结构及算法原理
- Python基础02 基本数据类型
- Python基础03 序列
- vim学习记录
- C语言编程时常犯十八个错误
- Linux架构与文件流
- Python基础04 运算
- html中加入虚线的方法