树形 dp
// ACM训练联盟周赛 C. Teemo's tree problem
There is an apple tree in Teemo's yard. It contains n nodes and n-1 branches, and the node 1 is always the root of the tree. Today, Teemo's father will go out for work. So Teemo should do his father's job in the family: Cut some branches to make the tree more beautiful. His father's told him that he should cut some branches, finally, the tree should just contains q branches. But when Teemo start to cut, he realizes that there are some apples in the branches( For example, there are 10 apples in the branches which connecting node 1 and node 4). So Teemo not only wants to achieve his father's order, but also wants to preserve apples as much as possible. Can you help him?
2 5
\ /
3 4
\ /
1
Input Format
- The first line of the input contains an integer T(1<=T<=10) which means the number of test cases.
- For each test case, The first line of the input contains two integers n,q(3<=n<=100,1<=q<=n-1), giving the number of the node and the number of branches that the tree should preserve.
- In the next n-1 line, each line contains three integers u,v,w(1<=u<=n,1<=v<=n,u!=v,1<=w<=100000), which means there is a branch connecting node u and node v, and there are w apple(s) on it.
Output Format
Print a single integer, which means the maximum possible number of apples can be preserved.
样例输入
1
5 2
1 3 1
1 4 10
2 3 20
3 5 20
样例输出
21
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N 209
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
int t,n,q;
int u,v,w,head[N],val[N];
int num[N];//num[i]: 包含i在内及其后面所有点的个数
int dp[N][N]; //dp[i][j] :以i为“根”,取j个点可获的最大价值
struct Edge{
int from,to,nex,w;
}e[N];
int cnt;
void init()
{
mem(head,-);//-1,因为cnt初始化为了0
mem(val,);
mem(dp,);
mem(num,);
cnt=;
}
void add(int u,int v,int w){
e[cnt].from=u;
e[cnt].to=v;
e[cnt].w=w;
e[cnt].nex=head[u];
head[u]=cnt++;
}
void getval(int u){
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(val[v]==){//找过的不会在找了
val[v]=e[i].w;//除根节点外把边的值赋给点
getval(v);
}
}
}
int dfs(int u,int fa)
{
num[u]=;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(v==fa) continue;
num[u]+=dfs(v,u);
}
dp[u][]=val[u];//只有自己,因此val[1]==0 :去掉所有的边才会只有1
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(v==fa) continue;
for(int j=num[u];j>=;j--){//逆序,若正序比如 j==1 ,无法更新,后面的也会出现错误
for(int k=;k<j&&k<=num[v];k++){
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
}
}
}
return num[u];
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&q);
for(int i=;i<n-;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);//无向图
}
val[]=inf;//不是0就可以
getval();
val[]=;
dfs(,);
printf("%d\n",dp[][q+]);//q条边对应q+1个点
}
return ;
}
树形 dp的更多相关文章
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
- POJ2342 树形dp
原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...
- hdu1561 The more, The Better (树形dp+背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...
- bzoj2500: 幸福的道路(树形dp+单调队列)
好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...
- BZOJ 1040 树形DP+环套树
就是有n个点n条边,那么有且只有一个环那么用Dfs把在环上的两个点找到.然后拆开,从这条个点分别作树形Dp即可. #include <cstdio> #include <cstrin ...
随机推荐
- split()分割字符串用法
<script type="text/javascript"> var str="How are you doing today?" documen ...
- 图像处理库CImg
CImg 是一个用C++编写的开源数字图像处理库. 作者介绍 作者David Tschumperlé, 之前是法国La Rochelle大学的一名教授,现受雇于CNRS 图像组.据说作者从19 ...
- 一、使用MyBatis
定义sql映射xml文件 userMapper.xml文件的内容如下: <!--头文件--> <!DOCTYPE mapper PUBLIC "-//mybatis.org ...
- 项目在cocos 2.23移植到cocos 3.1.0所出现的bug
在建项目时一定要注意选择源代码!而不是预编译库 "extensions/ExtensionMacros.h”: No such file 项目右键-属性-配置属性-c/c++ - 常规-附加 ...
- Python实现扫描作业配置自动化
持续集成平台接入扫描作业是一项繁琐而又需要细致的工作,于是趁着闲暇时间,将代码扫描作业用Python代码实现了配置自动化. 每次配置作业的过程中,都会在checkcode1或者checkcode3上 ...
- VC操作WORD文档总结
一.写在开头 最近研究word文档的解析技术,我本身是VC的忠实用户,看到C#里面操作WORD这么舒服,同时也看到单位有一些需求,就想尝试一下,结果没想到里面的技术点真不少,同时网络上的共享资料很多, ...
- ScriptManager对象的属性
--<本文属于摘抄> 属性 说明 EnablePageMethods 指定在ASPX页面上定义的公共静态方法是否可以从客户端脚本中作为Web服务方法调用 EnablePartialRend ...
- codevs 1683 车厢重组(水题日常)
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description 在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥墩水平旋转.一个车站的职工 ...
- 如何诊断 11.2 集群节点驱逐问题 (文档 ID 1674872.1)
适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 到 11.2.0.2 [发行版 11.2]本文档所含信息适用于所有平台 用途 这篇文档提 ...
- windows定时任务小注
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...