【树形DP】 HDU 1561 The more, The Better
来源:互联网 发布:美国石油出口解禁 知乎 编辑:程序博客网 时间:2024/05/18 09:51
经典树形DP
#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <iostream>#include <algorithm>#include <sstream>#include <cmath>using namespace std;#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#include <time.h>;#define cler(arr, val) memset(arr, val, sizeof(arr))#define FOR(i,a,b) for(int i=a;i<=b;i++)#define IN freopen ("in.txt" , "r" , stdin);#define OUT freopen ("out.txt" , "w" , stdout);typedef long long LL;const int MAXN = 201;const int MAXM = 201;const int INF = 0x3f3f3f3f;const int mod = 1000000007;struct node{ int v,next;} edge[MAXM];int n,m;int head[MAXM],tol,val[MAXN],dp[MAXN][MAXN];void init(){ cler(val,0); cler(head,-1); tol=0;}void addedge(int u,int v){ edge[tol].v=v,edge[tol].next=head[u]; head[u]=tol++;}void dfs(int u){ for(int i=0; i<=m; i++) dp[u][i]=-INF; dp[u][1]=val[u]; for(int i=head[u]; ~i; i=edge[i].next) { int v=edge[i].v; dfs(v); for(int j=m; j>=0; j--) for( int k=0; k<=j; k++) dp[u][j]=max(dp[u][j],dp[v][k]+dp[u][j-k]); }}int main(){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin);#endif while(~scanf("%d%d",&n,&m),n+m) { init(); for(int i=1; i<=n; i++) { int a; scanf("%d %d",&a ,&val[i] ); addedge(a,i); } m++; dfs(0); printf("%d\n",dp[0][m]); }}
0 0
- hdu 1561 The more, The Better (树形DP)
- hdu 1561 The more, The Better 树形DP+背包
- hdu 1561 The more, The Better(树形dp)
- hdu 1561 (树形dp)The more, The Better
- HDU 1561 The more, The Better(树形DP)
- hdu 1561 The more, The Better(树形DP入门)
- HDOJ (HDU) 1561 The more, The Better (树形DP)
- hdu 1561 The more, The Better 树形DP入门题
- 【树形DP】 HDU 1561 The more, The Better
- hdu 1561 The more, The Better (树形背包dp)
- HDU 1561 The more, The Better 依赖背包+树形DP
- hdu 1561 The more, The Better (树形DP)
- hdu 1561 The more, The Better(经典树形DP)
- hdu 1561 The more, The Better (树形dp)
- hdu 1561 The more, The Better (树形dp)
- HDU 1561 The more, The Better / 树形DP
- HDU 1561 The more, The Better (树形dp)
- hdu 1561 The more, The Better(树形DP)
- 【转】5种服务器网络编程模型讲解
- POJ 1035 Spell checker
- 在O(1)的空间复杂度内,打印螺旋数组
- 不同浏览器对于html5 audio标签和音频格式的兼容性
- 黑马程序员——银行业务调度系统
- 【树形DP】 HDU 1561 The more, The Better
- NYOJ 815 三角形【海伦公式】
- 反射
- 选择排序
- 【蓝牙低功耗BLE】引言
- C++中getline函数用法
- 天圆地方· 围棋界的盲棋天才 -- 鲍云
- 黑马程序员——深入理解C#的事件模型
- 关于ORA-12170: TNS: 连接超时解决方法