题目链接 : 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. 解决yii2 禁用layout时AppAsset不加载资源的问题

    大王派我来巡山_site:http://blog.csdn.net/wang78699425/article/details/52369841 最近由于项目(yii2 的一个项目)需要,登录页面不需要 ...

  2. oracle获取连续时间

    SELECT rownum, (to_date('2015-01-01', 'yyyy-mm-dd') + rownum - 1) AS show_time FROM dualCONNECT BY r ...

  3. 1.4 安装Linux系统

    按F2进入BIOS,设置通过[CD/ROM]启动,如果是真实计算机,安装完后还需要重新设置为[硬盘启动] 设置分区如下图所示:

  4. Spring Boot 入门之基础篇(一)

    原文地址:Spring Boot 入门之基础篇(一) 博客地址:http://www.extlight.com 一.前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是 ...

  5. C# 流总结

    前言 本篇文章简单总结了在C#编程中经常会用到的一些流.比如说FileStream.MemoryStream. BufferedStream. NetWorkStream. StreamReader/ ...

  6. android 监控应用进程

    在android系统中,怎么监控应用的进程改变及消亡呢? 至于监控应用进程能做什么,这个就不多说了,你懂的. 在android系统中有这么一个类ActivityManagerNative,看名称就大概 ...

  7. wps表格开发C#

    1.需要添加引用etapi.dll,这个dll在你的wps的安装目录下面可以找到. 2.主要的类: Excel.Application:顶层对象 WorkBook:工作簿 WorkSheet:表 Ra ...

  8. 第25课 可变参数模板(6)_function_traits和ScopeGuard的实现

    1. function_traits (1)function_traits的作用:获取函数的实际类型.返回值类型.参数个数和具体类型等.它能获取所有函数语义类型信息.可以获取普通函数.函数指针.std ...

  9. Vue 目录结构 绑定数据 绑定属性 循环渲染数据

    一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...

  10. IntelliJ IDEA 常用设置

    1.idea每次打开项目的设置 2.设置编译器的快捷键  File->Keymap 在列表里面选择快捷键的名称  如果想使用跟Eclipse一样的快捷键,选中Eclipse copy即可 3.滚 ...