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 ...
随机推荐
- RabbitMQ二:AMQP协议
参考这个:http://kb.cnblogs.com/page/73759/ 参考这个:http://www.cnblogs.com/charlesblc/p/6286875.html 写的挺好 Ra ...
- Spring Boot (32) Lock 本地锁
平时开发中,有时会双击提交表单造成重复提交,或者网速比较慢时还没有响应又点击了按钮,我们在开发中必须防止重复提交 一般在前台进行处理,定义个变量,发送请求前判断变量值为true,然后把变量设置为fal ...
- Spring注解的步骤
Spring框架提供DI(属性注解)和IOC(类/Bean的注解)注解. 注解:标注.注入和解析.解释;标注和解释一部分代码的作用在框架中:就是配置文件的另外一种实现方式@Type.@Taget;减少 ...
- DeepMind:所谓SACX学习范式
机器人是否能应用于服务最终还是那两条腿值多少钱,而与人交互,能真正地做"服务"工作,还是看那两条胳膊怎么工作.大脑的智能化还是非常遥远的,还是先把感受器和效应器做好才是王道. 关于 ...
- 10.5 集合ArrayList 和 io流
1.ArrayListToFile package day10_io_fileWrite_Read.arraylist_tofile; import java.io.BufferedWriter; i ...
- linux ssh 利用scp传输文件
使用方式如下: 1.上传本地文件到服务器 scp /path/filename username@servername:/path/ 例如scp /var/www/test.php root@192. ...
- 00Enterprise Resource Planning
Enterprise Resource Planning 企业资源计划即 ERP (Enterprise Resource Planning),由美国 Gartner Group 公司 ...
- 解决docker容器启动时候无法映射端口的问题
当我们停止防火墙后,docker容器启动映射端口可能无法映射端口,这个时候需要重建docker0网桥. 详细的错误是这样的: docker: Error response from daemon: d ...
- Invalid ON UPDATE clause for 'create_date' column
高版本的mysql导数据到低版本出现的问题 日期类型报错 解决方式:将datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 中的 ON ...
- c++函数进阶
c++扩展了c语言的函数功能.通过将incline关键字用于函数定义,并在首次调用该函数前提供其函数定义,可以使得c++编译器将该函数视为内联函数.也就是说,编译器不是让程序跳到独立的代码段,以执行函 ...