题目链接 : https://nanti.jisuanke.com/t/29228

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

题意是有一棵以 1号点为根节点的 n个结点的树, n-1 条边均有权值,现在把这棵树在保留根节点的情况下剪成一棵 q条边的树并且使剩余的树权值最大。(注意 : 减去一条边该边后面的边都会被去掉)

这应该是一道十分经典的树形dp 。

除根节点外将 边的权值赋给点,val[i]记录i号点的权值。

have[i] 表示i号点及其之后的所有点的个数, dp[i][j]表示在i号点为"根"的情况下共保留j个点的最大权值。

做题时想到了边值赋点,却不知如何dp,树形dp还是见少了。

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back((x)) typedef long long ll;
const int INF=0x3f3f3f3f;
struct Edge{
int to;
int wei;
Edge(int v,int w):to(v),wei(w) {}
};
vector< Edge > G[];
int val[];
int have[];
int dp[][]; void getVal(int u){
for( auto e : G[u]){
if(val[e.to]==){
val[e.to]=e.wei;
getVal(e.to);
}
}
} int dfs(int u,int fa){
have[u]=;
for( auto e : G[u]){
if(e.to==fa) continue;
have[u]+=dfs(e.to,u);
}
dp[u][]=val[u];
for( auto e : G[u]){
if(e.to==fa) continue;
for(int tot=have[u];tot>=;tot--){
for(int i=;i<tot&&i<=have[e.to];++i){
dp[u][tot]=max(dp[u][tot],dp[u][tot-i]+dp[e.to][i]);
}
}
}
return have[u];
} int main(){
int T;
scanf("%d",&T);
while(T--){
int N,rmn;
scanf("%d%d",&N,&rmn);
for(int i=;i<=N;++i) G[i].clear();
for(int i=;i<N-;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].pb(Edge(v,w));
G[v].pb(Edge(u,w));
}
memset(val,,sizeof(val));
memset(have,,sizeof(have));
memset(dp,,sizeof(dp));
val[]=INF;
getVal();
/*
for(int i=1;i<=N;++i)
printf("%d : %d\n",i,val[i]);
*/
val[]=;
dfs(,);
int ans=dp[][rmn+];
printf("%d\n",ans);
}
return ;
}

Teemo's tree problem的更多相关文章

  1. ZOJ 3686 A Simple Tree Problem

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

  2. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  3. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  4. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  5. [Algorithm] Universal Value Tree Problem

    A unival tree (which stands for "universal value") is a tree where all nodes under it have ...

  6. ZOJ 3686 A Simple Tree Problem(线段树)

    Description Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the ...

  7. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  8. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  9. 【点分树】codechef Yet Another Tree Problem

    已经连咕了好几天博客了:比较经典的题目 题目大意 给出一个 N 个点的树和$K_i$, 求每个点到其他所有点距离中第 $K_i$ 小的数值. 题目分析 做法一:点分树上$\log^3$ 首先暴力做法: ...

随机推荐

  1. node day2 vue read html

    app.js var http = require("http"); var fs = require('fs'); var url = require('url'); http. ...

  2. JS 验证字符串是否为空

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. linux服务器架设--学习笔记

    PS: Centos是属于红帽子的操作系统

  4. C++对象的构造、析构与拷贝构造

    今天下午在研究虚函数的时候遇到了一个问题,觉得很有意思,记录一下. 先看代码: class Base { public: Base(int value) { m_nValue = value; cou ...

  5. zombodb 几点说明

    内容来自官方文档,截取部分 默认es 索引的副本为0 这个参考可以通过修改索引,或者在创建的时候通过with 参数指定,或者通过pg 的配置文件中指定 索引更多的列以为这使用了更多的es 能力 索引的 ...

  6. JAVA工程师面试题库

    这些都是从其他地方copy过来的,如有侵权的话,可以联系我下架.这期只有问题,后面我会整理答案再重新发出来. http://blog.csdn.net/jackfrued/article/detail ...

  7. 这台计算机上缺少此项目引用的 NuGet 程序包,DotNetCompilerPlatform

    严重性 代码 说明 项目 文件 行 禁止显示状态错误 这台计算机上缺少此项目引用的 NuGet 程序包.使用“NuGet 程序包还原”可下载这些程序包.有关更多信息,请参见 http://go.mic ...

  8. buffers和cached的区别

    原文:https://www.cnblogs.com/kevingrace/p/5991604.html buffers和cached解释 ============================== ...

  9. VS2017调试出现异常浏览器直接关闭的解决办法

    最近升级完VS2017后,出现了各种不适应. 1.F5调试时总是会打开新的浏览器,过去都是在现有窗口右侧打开新的新的浏览器标签页. 这一点就让很不爽,勉强接受吧,继续调试代码但是还有第二种情况. 2. ...

  10. 受限玻尔兹曼机(Restricted Boltzmann Machine, RBM) 简介

    受限玻尔兹曼机(Restricted Boltzmann Machine,简称RBM)是由Hinton和Sejnowski于1986年提出的一种生成式随机神经网络(generative stochas ...