Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

Description

You're given a tree with weights of each node, you need to find the maximum subtree of specified size of this tree.

Tree Definition

A tree is a connected graph which contains no cycles.

Input

There are several test cases in the input.

The first line of each case are two integers N(1 <= N <= 100), K(1 <= K <= N), where N is the number of nodes of this tree, and K is the subtree's size, followed by a line with N nonnegative integers, where the k-th integer indicates the weight of k-th node.
The following N - 1 lines describe the tree, each line are two integers which means there is an edge between these two nodes. All indices above are zero-base and it is guaranteed that the description of the tree is correct.

Output

One line with a single integer for each case, which is the total weights of the maximum subtree.

Sample Input

  1. 3 1
  2. 10 20 30
  3. 0 1
  4. 0 2
  5. 3 2
  6. 10 20 30
  7. 0 1
  8. 0 2

Sample Output

  1. 30
  2. 40

Source

ZOJ Monthly, May 2009





树状DP~

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7. #define maxn 105
  8. int n,k,dp[maxn][maxn],val[maxn];
  9. vector <int> edge[maxn];
  10. void dfs(int u,int y)
  11. {
  12. dp[u][1] = val[u];
  13. for(int i = 0;i < edge[u].size();i ++)
  14. {
  15. int v = edge[u][i];
  16. if(v == y) continue;
  17. dfs(v, u);
  18. for(int j = k;j > 0;j --) //相似01背包
  19. for(int p = 0;p < j;p ++)
  20. {
  21. dp[u][j] = max(dp[u][j], dp[u][j-p] + dp[v][p]);
  22. }
  23. }
  24. }
  25. int main()
  26. {
  27. int a, b,ans;
  28. while(scanf("%d%d", &n, &k) != EOF)
  29. {
  30. ans = -1;
  31. memset(dp, -1, sizeof(dp));
  32. for(int i = 0;i < n;i ++)
  33. edge[i].clear();
  34. for(int i = 0;i < n;i ++)
  35. scanf("%d", &val[i]);
  36. for(int i = 0;i < n-1;i ++)
  37. {
  38. scanf("%d%d",&a,&b);
  39. edge[a].push_back(b);
  40. edge[b].push_back(a);
  41. }
  42. dfs(0, -1);
  43. for(int i = 0;i < n;i ++)
  44. ans = max(ans, dp[i][k]);
  45. printf("%d\n", ans);
  46. }
  47. return 0;
  48. }

ZOJ 3201的更多相关文章

  1. ZOJ 3201 Tree of Tree

    树形DP.... Tree of Tree Time Limit: 1 Second      Memory Limit: 32768 KB You're given a tree with weig ...

  2. ZOJ 3201 树形dp+背包(简单题)

    #include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...

  3. ZOJ - 3201 Tree of Tree (树形背包)

    题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...

  4. ZOJ 3201 树形背包问题

    题目大意: 0~n-1号这n个点,每个点有个权值,由无向边形成了一棵树,希望在这棵树上找到一棵长为m的子树使总的权值最小 基本的树形背包问题 令dp[u][j] 表示u号节点对应子树中有j个节点所能得 ...

  5. dp (1)

    D - Tree of Tree ZOJ - 3201 这个题目我开始是这么定义的dp[i][j][0] dp[i][j][1] 表示对于第i个节点还有j个的选择 0 代表不选这个节点,1 代表选这个 ...

  6. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  7. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  8. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  9. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

随机推荐

  1. java 分解整数 【个 十 百】(数组案例)

    求一个数两位数的个位数,十位数,百位数及千位: int num = 53; int g = (num / 1) % 10;  //个位 int s = (num / 10) % 10; //十位 in ...

  2. IKanalyzer、ansj_seg、jcseg三种中文分词器的实战较量

    转自:http://lies-joker.iteye.com/blog/2173086 选手:IKanalyzer.ansj_seg.jcseg 硬件:i5-3470 3.2GHz 8GB win7 ...

  3. vs2017 创建C#类时添加文件头

    C#类模板地址:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\C ...

  4. jstree的基本使用例子

    var menu = (function() { var _menu = {data:{}, initMenu : function() { $.jstree.defaults.core.themes ...

  5. 14Oracle Database 高级事务,游标

    Oracle Database 高级事务,游标 隔离级别 脏读 不可重复读 虚读 读未提交 Read uncommitted 可以 可以 可以 读已提交 Read committed 不可以 可以 可 ...

  6. docker 1-->docker swarm 转载

    实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...

  7. 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用

    目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...

  8. MySQL练习题及答案(复习)

    新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...

  9. Linux:SAMBA共享、NFS共享、Autofs自动挂载

    SAMBA.NFS共享区别 NFS开源文件共享程序:NFS(NetworkFile System)是一个能够将多台Linux的远程主机数据挂载到本地目录的服务,属于轻量级的文件共享服务,不支持Linu ...

  10. Django加载静态文件失败,已解决

    1.css文件以及js文件要放在static目录下,static和templates属于同级目录 2.在Django项目的同名项目文件的setting.py中,最后添加静态文件夹static目录路径 ...