题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018

Dynamic Programming. 首先要根据input建立树形结构,然后在用DP来寻找最佳结果。dp[i][j]表示node i的子树上最多保存j个分支的最佳结果。代码如下:

  1. #include <iostream>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #include <string.h>
  7. #include <string>
  8. #include <sstream>
  9. #include <cstring>
  10. #include <queue>
  11. #include <vector>
  12. #include <functional>
  13. #include <cmath>
  14. #include <set>
  15. #define SCF(a) scanf("%d", &a)
  16. #define IN(a) cin>>a
  17. #define FOR(i, a, b) for(int i=a;i<b;i++)
  18. #define Infinity 9999999
  19. typedef long long Int;
  20. using namespace std;
  21.  
  22. int dp[][]; //row -> node id, col -> branches preserved.
  23.  
  24. struct link {
  25. int parent;
  26. int child;
  27. int weight;
  28. };
  29.  
  30. struct tree {
  31. int parent;
  32. tree *left, *right;
  33. int leftw, rightw;
  34. };
  35.  
  36. int N, Q;
  37. link links[];
  38. bool visited[];
  39.  
  40. tree * construct(int pid)
  41. {
  42. tree *t = new tree;
  43. t->parent = pid;
  44. int num = ;
  45. bool found = false;
  46. FOR(i, , N - )
  47. {
  48. if (visited[i] == false && (links[i].parent == pid || links[i].child==pid))
  49. {
  50. visited[i] = true;
  51. if (num == )
  52. {
  53. t->leftw = links[i].weight;
  54. if(links[i].parent==pid)
  55. t->left = construct(links[i].child);
  56. else
  57. t->left = construct(links[i].parent);
  58. num++;
  59. }
  60. else if (num == )
  61. {
  62. t->rightw = links[i].weight;
  63. if(links[i].parent==pid)
  64. t->right = construct(links[i].child);
  65. else
  66. t->right = construct(links[i].parent);
  67. found = true;
  68. break;
  69. }
  70. }
  71. }
  72. if (found == false)
  73. {
  74. t->left = NULL;
  75. t->right = NULL;
  76. }
  77. return t;
  78. }
  79.  
  80. int calcuTree(tree *t, int R)
  81. {
  82. if (t == NULL)
  83. return ;
  84.  
  85. int id = t->parent;
  86. if (dp[id][R] == -)
  87. {
  88. int maxLeft = ;
  89. for (int leftR = ; leftR <= R; leftR++)
  90. {
  91. int cleft = ;
  92. int rightR = R - leftR;
  93. if (leftR > )
  94. {
  95. cleft += t->leftw;
  96. cleft += calcuTree(t->left, leftR - );
  97. }
  98. if (rightR > )
  99. {
  100. cleft += t->rightw;
  101. cleft += calcuTree(t->right, rightR - );
  102. }
  103.  
  104. if (cleft > maxLeft)
  105. maxLeft = cleft;
  106. }
  107. dp[id][R] = maxLeft;
  108. }
  109. return dp[id][R];
  110. }
  111.  
  112. int main()
  113. {
  114. while (scanf("%d %d\n", &N, &Q) != EOF)
  115. {
  116. FOR(i, , N - )
  117. {
  118. int p, c, w;
  119. scanf("%d %d %d", &links[i].parent, &links[i].child, &links[i].weight);
  120. }
  121.  
  122. FOR(i, , N - )
  123. visited[i] = false;
  124.  
  125. tree *t = new tree;
  126. t = construct();
  127.  
  128. FOR(i, , N + )
  129. {
  130. FOR(j, , Q + )
  131. {
  132. dp[i][j] = -;
  133. }
  134. }
  135.  
  136. FOR(i, , N + )
  137. {
  138. dp[i][] = ;
  139. }
  140.  
  141. int ans = calcuTree(t, Q);
  142. printf("%d\n", ans);
  143. }
  144. return ;
  145. }

Ural 1018 Binary Apple Tree的更多相关文章

  1. CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)

    CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...

  2. URAL 1018 Binary Apple Tree(树DP)

    Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...

  3. ural 1018 Binary Apple Tree(树形dp | 经典)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  4. timus 1018. Binary Apple Tree

    1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...

  5. BNUOJ 13358 Binary Apple Tree

    Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...

  6. 【URAL 1018】Binary Apple Tree

    http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...

  7. URAL1018 Binary Apple Tree(树形DP)

    题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...

  8. URAL1018 Binary Apple Tree(树dp)

    组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...

  9. URAL1018. Binary Apple Tree

    链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...

随机推荐

  1. git比较两个分支的文件的差异

    Git diff branch1 branch2 --stat   //显示出所有有差异的文件列表 Git diff branch1 branch2 文件名(带路径)   //显示指定文件的详细差异 ...

  2. VS在.NETFramework升级时遇到类库冲突如何解决

    相信大家在开发环境中随着程序的不断升级,很多时间需要升级. NETFramework版本.今天项目中遇到的问题是从. NETFramework4.0升级到4.5时提示 Entityframework. ...

  3. 普通PC机支持内存128G,单条32G内存

    以前,不管是英特尔还是AMD的消费级平台支持内存容量大多都是64GB,这一现状被英特尔公司去年推出的第九代酷睿而改变.第九代酷睿最大支持128GB内存,虽然只是简单的提高了内存容量,对大多数电脑用户而 ...

  4. (转)适用微信小程序的table表格(带隔行变色)

    原文地址 table.wxml <view class="table"> <view class="tr bg-w"> <view ...

  5. JVM内存区域详解(Eden Space、Survivor Space、Old Gen、Code Cache和Perm Gen)

    JVM区域总体分两类,heap区和非heap区.heap区又分为: Eden Space(伊甸园). Survivor Space(幸存者区). Old Gen(老年代). 非heap区又分: Cod ...

  6. 学习excel的使用技巧三快捷键和思路

    快捷键 CRTL+回车 是多行执行 思路 关于公式 在空白出 写= 即开始写公式 excel第一行 就是行标 比如 A1 就是excel 表格中第一个 比如来个乘法 =A1*12+b1*13 求和更简 ...

  7. 学习:D3

    http://www.ourd3js.com/wordpress/?p=196 http://www.ourd3js.com/demo/rm/R-9.2/force.html 力导向图(那个可以拖拽的 ...

  8. OpenGL中投影矩阵基础知识

    投影矩阵元素Projection Matrix 投影矩阵构建: 当f趋向于正无穷时: 一个重要的事实是,当f趋于正无穷时,在剪裁空间中点的z坐标跟w坐标相等.计算方法如下: 经过透视除法后,z坐标变为 ...

  9. 多进程模块 multiprocessing

    由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiprocessing包是Python中的多进程 ...

  10. HTML5-canvas1.0

    HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图形容器,您必须使用脚本来绘制图形.你可以通过多种方 ...