Teemo's tree problem
题目链接 : 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的更多相关文章
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- xtu数据结构 I. A Simple Tree Problem
I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- [Algorithm] Universal Value Tree Problem
A unival tree (which stands for "universal value") is a tree where all nodes under it have ...
- 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 ...
- ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
- zoj 3686 A Simple Tree Problem (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- 【点分树】codechef Yet Another Tree Problem
已经连咕了好几天博客了:比较经典的题目 题目大意 给出一个 N 个点的树和$K_i$, 求每个点到其他所有点距离中第 $K_i$ 小的数值. 题目分析 做法一:点分树上$\log^3$ 首先暴力做法: ...
随机推荐
- PHP处理上传文件信息数组中的文件类型 正确获取
PHP处理上传文件信息数组中的文件类型$_FILES['type']由客户端浏览器提供,有可能是黑客伪造的信息,请写一个函数来确保用户上传的图像文件类型真实可靠 如果是一般文件则通过 mime_con ...
- 工作中 sql 整理(一)
这篇文章记录关于SQL的内容,有些凌乱,是工作中点滴的积累,只能按照时间顺序,逐次记录. 一.update 关联更新 1.需求 Table A TableB A表中的主键和B表中的主键相关联,关联 ...
- Restful Service 中 DateTime 在 url 中传递
在C# url 中一旦包特殊字符,请求可能就无法送达.可以使用如下方法,最为便捷. 请求端: beginTime.Value.ToString("yyyyMMddHHmmss") ...
- 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ
========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...
- 什么是pytorch(2Autograd:自动求导)(翻译)
Autograd: 自动求导 pyTorch里神经网络能够训练就是靠autograd包.我们来看下这个包,然后我们使用它来训练我们的第一个神经网络. autograd 包提供了对张量的所有运算自动求导 ...
- Python的paramiko,实现ssh
最简单的使用paramiko登录远程机器执行一些命令,学习实验楼的paramiko记录下来,第一次使用ConfigParser这个库,对于封装这些还是不太熟悉,只能慢慢来,嘿嘿嘿 这是python脚本 ...
- Day 09 函数基础
函数初级 简介 # 函数是一系列代码的集合,用来完成某项特定的功能 优点 '''1. 避免代码的冗余2. 让程序代码结构更加清晰3. 让代码具有复用性,便于维护''' 函数四部分 '''1. 函数名: ...
- 血红蛋白值的临床意义(hemoglobin ,Hb,HGB)
血红蛋白临床意义: 血红蛋白增高.降低的临床意义基本和红细胞计数的临床意义相似,但血红蛋白能更好地反映贫血的程度. 血红蛋白增多有以下情况: (1)生理性增多:见于高原居民.胎儿和新生儿,剧烈活动 ...
- VirtualBox网络的Host-Only配置
创建host-only虚拟网卡 VBox管理器页面-管理-主机网络管理器,如果已经存在默认的虚拟网卡则下一步,如果不存在则创建一个虚拟网卡,不启用DHCP服务器,这里ip地址为192.168.137. ...
- Vue--生命周期函数
生命周期函数就是组件挂载.以及组件销毁的时候触发的一系列方法,这些方法叫做生命周期函数: beforeCreate(){ console.log('实例创建之前-1') }, created(){ c ...