题目链接 : 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. angular引用echarts插件

    方法一 1. 命令行下载 npm install echarts --savenpm install ngx-echarts --save 2. angular.json 配置echarts路径. 2 ...

  2. python写入txt文件时的覆盖和追加

    python写入文件时的覆盖和追加 在使用Python进行txt文件的读写时,当打开文件后,首先用read()对文件的内容读取,然后再用write()写入,这时发现虽然是用"r+" ...

  3. COMBIN14简单应用

    目录 案例1 说明 APDL代码 结果 案例2 说明 APDL代码 结果 案例3 说明 APDL代码 结果 参考网址:http://blog.sina.com.cn/s/blog_65936c2301 ...

  4. AIUI开放平台:多轮对话返回前几轮语槽数据

    编写云函数: AIUI.create("v2", function(aiui, err){ // 获取 response response = aiui.getResponse() ...

  5. [蓝桥杯]ALGO-186.算法训练_P0501

    输入两个无符号整数x, y, 用位操作实现无符号整数的乘法运算.不用考虑整数的溢出. 输入: 输出: 题目描述 代码如下: #include <stdio.h> #include < ...

  6. Faster R-CNN代码例子

    主要参考文章:1,从编程实现角度学习Faster R-CNN(附极简实现) 经常是做到一半发现收敛情况不理想,然后又回去看看这篇文章的细节. 另外两篇: 2,Faster R-CNN学习总结      ...

  7. 经典笔试题型----IT经理(IT Manager)

    一般企业设置IT部门都是服务性质,虽然谈IT需要成为战略部门许多年,但用脑子想下,这概率有多少?企业存在的第一目标是:赚取利润.贸易型企业最重要的部门为销售部,生产型企业最重要的部门为销售部与生产部, ...

  8. django之COOKIE 与 SESSION

    COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...

  9. mongo官方企业版安装及数据库授权使用

    通过安装.deb包的方式,系统是Ubuntu 16.04 1. Import the public key used by the package management system.(导入包管理系统 ...

  10. postgresql模糊查询json类型字段内某一属性值

    需求场景: 目录以jsonb格式存储在数据库表t的chapter字段中,需要菜单路径中包含指定字符串(比如“语文”或者“上学期”)的menu 以下为chapter字段存储json示例: { " ...