ZOJ 3201
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
- 3 1
- 10 20 30
- 0 1
- 0 2
- 3 2
- 10 20 30
- 0 1
- 0 2
Sample Output
- 30
- 40
Source
树状DP~
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- #define maxn 105
- int n,k,dp[maxn][maxn],val[maxn];
- vector <int> edge[maxn];
- void dfs(int u,int y)
- {
- dp[u][1] = val[u];
- for(int i = 0;i < edge[u].size();i ++)
- {
- int v = edge[u][i];
- if(v == y) continue;
- dfs(v, u);
- for(int j = k;j > 0;j --) //相似01背包
- for(int p = 0;p < j;p ++)
- {
- dp[u][j] = max(dp[u][j], dp[u][j-p] + dp[v][p]);
- }
- }
- }
- int main()
- {
- int a, b,ans;
- while(scanf("%d%d", &n, &k) != EOF)
- {
- ans = -1;
- memset(dp, -1, sizeof(dp));
- for(int i = 0;i < n;i ++)
- edge[i].clear();
- for(int i = 0;i < n;i ++)
- scanf("%d", &val[i]);
- for(int i = 0;i < n-1;i ++)
- {
- scanf("%d%d",&a,&b);
- edge[a].push_back(b);
- edge[b].push_back(a);
- }
- dfs(0, -1);
- for(int i = 0;i < n;i ++)
- ans = max(ans, dp[i][k]);
- printf("%d\n", ans);
- }
- return 0;
- }
ZOJ 3201的更多相关文章
- ZOJ 3201 Tree of Tree
树形DP.... Tree of Tree Time Limit: 1 Second Memory Limit: 32768 KB You're given a tree with weig ...
- ZOJ 3201 树形dp+背包(简单题)
#include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...
- ZOJ - 3201 Tree of Tree (树形背包)
题意:有一棵树,树上每个结点都有一个权值,求恰好包含k个结点的子树的最大权值. 设dp[i][j]为以结点i为根的树中包含j个结点的子树的最大权值,则可以把这个结点下的每棵子树中所包含的所有子树的大小 ...
- ZOJ 3201 树形背包问题
题目大意: 0~n-1号这n个点,每个点有个权值,由无向边形成了一棵树,希望在这棵树上找到一棵长为m的子树使总的权值最小 基本的树形背包问题 令dp[u][j] 表示u号节点对应子树中有j个节点所能得 ...
- dp (1)
D - Tree of Tree ZOJ - 3201 这个题目我开始是这么定义的dp[i][j][0] dp[i][j][1] 表示对于第i个节点还有j个的选择 0 代表不选这个节点,1 代表选这个 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1392 The Hardest Problem Ever
放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...
随机推荐
- java 分解整数 【个 十 百】(数组案例)
求一个数两位数的个位数,十位数,百位数及千位: int num = 53; int g = (num / 1) % 10; //个位 int s = (num / 10) % 10; //十位 in ...
- IKanalyzer、ansj_seg、jcseg三种中文分词器的实战较量
转自:http://lies-joker.iteye.com/blog/2173086 选手:IKanalyzer.ansj_seg.jcseg 硬件:i5-3470 3.2GHz 8GB win7 ...
- vs2017 创建C#类时添加文件头
C#类模板地址:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\C ...
- jstree的基本使用例子
var menu = (function() { var _menu = {data:{}, initMenu : function() { $.jstree.defaults.core.themes ...
- 14Oracle Database 高级事务,游标
Oracle Database 高级事务,游标 隔离级别 脏读 不可重复读 虚读 读未提交 Read uncommitted 可以 可以 可以 读已提交 Read committed 不可以 可以 可 ...
- docker 1-->docker swarm 转载
实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos 以及 Swarm 等众多容器集群系统,我们该如何选择 ...
- 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用
目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...
- MySQL练习题及答案(复习)
新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...
- Linux:SAMBA共享、NFS共享、Autofs自动挂载
SAMBA.NFS共享区别 NFS开源文件共享程序:NFS(NetworkFile System)是一个能够将多台Linux的远程主机数据挂载到本地目录的服务,属于轻量级的文件共享服务,不支持Linu ...
- Django加载静态文件失败,已解决
1.css文件以及js文件要放在static目录下,static和templates属于同级目录 2.在Django项目的同名项目文件的setting.py中,最后添加静态文件夹static目录路径 ...