poj 2342 树形dp
2015-07-15 11:04
302 查看
<span style="font-size:18px;">题意:有n个人,接下来n行是n个人的价值,再接下来n行给出l,k,k是l的上司,问有直接隶属关系不能同时出现的条件下 能达到的价值最大和 思路:树形dp 其实就是优化了的dfs ; 先找到根,再递归每个节点 使其作为根 并求出其当前最大的价值和,状态方程如下: 当i来的时候,dp[i][1] += dp[j][0];//j为i的下属 当i不来的时候,dp[i][0] +=max(dp[j][1],dp[j][0]);//j为i的下属 代码: #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <stdlib.h> #include <string.h> #include <iomanip> #define N 6010 #define INF 10000000 #define LL long long #define eps 10E-9 #define mem(a) memset(a,0,sizeof(a)) #define w(a) while(a) #define s(a) scanf("%d",&a) #define ss(a,b) scanf("%d%d",&a,&b) #define sss(a,b,c) scanf("%lld%lld%lld",&a,&b,&c) #define MAXN 9999 #define MAXSIZE 10 #define DLEN 4 #define MAXN 9999 #define MAXSIZE 10 #define DLEN 4 using namespace std; int dp [2]; int vis ,dad ; int n; void tree_dp(int node){ vis[node]=1; for(int i=1; i<=n; i++){ if(!vis[i] && dad[i]==node){ tree_dp(i); dp[node][1] += dp[i][0]; dp[node][0] += max(dp[i][0], dp[i][1]); } } } int main() { w(~s(n)){ mem(dp); mem(dad); mem(vis); for(int i=1; i<=n; i++){ s(dp[i][1]); } int e, c, root = 0; w(ss(e,c) && (e|c)){ dad[e] = c; if(root = e){ root = c; } } w(dad[root]){ root = dad[root]; } tree_dp(root); cout<<max(dp[root][0],dp[root][1])<<endl; } return 0; } </span>
相关文章推荐
- 自动调节图像的对比度 和改变图像的大小
- GRE写作必备句型
- 详解clientHeight、offsetHeight、scrollHeight
- SystemUI源码分析
- android 中的组合控件的设计
- xml的第一次小试牛刀
- JADX 的使用方法 。
- Unity3D中的C#事件
- 怎么样不利用第三变量交换x,y
- jQuery使用方法简介
- 证明 poj 1014 模优化修剪,部分递归 有错误
- themeforest 模板
- linux下用phpize给PHP动态添加扩展
- Spring MVC 中 引入 Mybatis 的实现方法(一)
- r语言学习(2)
- Linux怎么查看系统开机时间
- 最值得阅读学习的 10 个 C 语言开源项目代码
- [TOJ 4108] Hearthstone
- 第十三周
- U3D 接GooglePlay in-app-billing支付注意事项