// 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?

 
 
 
 
 
1
2   5
2
 \ / 
3
  3   4
4
   \ /
5
    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的更多相关文章

  1. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  2. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  3. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  4. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  5. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  6. BZOJ 2286 消耗战 (虚树+树形DP)

    给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...

  7. POJ2342 树形dp

    原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...

  8. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

  9. bzoj2500: 幸福的道路(树形dp+单调队列)

    好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...

  10. BZOJ 1040 树形DP+环套树

    就是有n个点n条边,那么有且只有一个环那么用Dfs把在环上的两个点找到.然后拆开,从这条个点分别作树形Dp即可. #include <cstdio> #include <cstrin ...

随机推荐

  1. laravel-admin 自定义导出表单

    官方导出文档 laravel-admin自带的导出excel会导出与此模型关联的其他数据.所以参考官方文档调整代码 文章表:id,title,user_id 用户表:id,username //文章模 ...

  2. Qt 2D绘图之二:抗锯齿渲染和坐标系统

    一.抗锯齿渲染 1.1 逻辑绘图 图形基元的大小(宽度和高度)始终与其数学模型相对应,下图示意了忽略其渲染时使用的画笔的宽度的样子. 1.2 物理绘图(默认情况) 在默认的情况下,绘制会产生锯齿,并且 ...

  3. 牛客寒假6-C.项链

    链接:https://ac.nowcoder.com/acm/contest/332/C 题意: 小B想给她的新项链染色. 现在有m种颜色,对于第i种颜色,小B有a_i单位的颜料,每单位颜料可以染项链 ...

  4. VMWare版本兼容问题处理

    其他电脑拷贝过来的VMWare虚拟机启动时报错: 在虚拟机根目录中找到"vmx"后缀文件,发现原来的VM版本为14,而本地的VMWare版本为11. virtualHW.versi ...

  5. ORA-00972_标识符过长

    执行SQL查询报:"ORA-00972:标识符过长"错误. 执行SQL: SELECT T.F_FTBS, T.F_TZMC "X组/XXXX/XXXX名称", ...

  6. python+selenium+requests爬取qq空间相册时遇到的问题及解决思路

    最近研究了下用python爬取qq空间相册的问题,遇到的问题及解决思路如下: 1.qq空间相册的访问需要qq登录并且需是好友,requests模块模拟qq登录略显麻烦,所以采用selenium的dri ...

  7. 关于Chrome和Opera中draw Image()方法无法在canvas画布中绘制图片的问题

    var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var img=d ...

  8. 牛客网Java刷题知识点之什么是cookie、什么是session、cookie和session有什么区别

    不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?tpId=31&tqId=21170&query=&asc= ...

  9. 解决“程序包管理器控制台”输入命令找不到Nuget包问题

    问题: 问题原因: Nuget源的地址上不去 解决办法: 1.将Nuget源更新为可以国内使用的官方Nuget源. 1)打开VS2013:工具-->Nuget程序包管理器-->程序包管理器 ...

  10. webpack 精华文章

    最近迁移 webpack1 到 webpack3 碰到了一些问题,还是通过一些文章,解决了问题.在这里做一个备份,方便以后使用. 从零搭建vue2+vue-router2+webpack3工程 Web ...