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$ 首先暴力做法: ...
随机推荐
- How to create an rpm package
转自:https://linuxconfig.org/how-to-create-an-rpm-package Rpm is both the package manager and the pack ...
- zabbix_agent添加到系统服务启动(八)
Centos6.5上安装了zabbix_agent后,需要把zabbix_agent添加到系统服务启动,要不然每次要一长串路径再启动,挺麻烦的. 步骤: 1)拷贝zabbix解压包里的zabbix_a ...
- 高分辨率下放大netbeans中的小图标
参考:http://ask.csdn.net/questions/388953 在高DPI下,Netbeans 8.2的图标变得非常小.怎么办? 修改C:\Program Files\NetBeans ...
- ftp 和vsftp
内置sftp:https://blog.csdn.net/xinxin19881112/article/details/46831311 vsftp:http://blog.51cto.com/cui ...
- Spring Boot使用单元测试
一.Service层单元测试: 代码如下: package com.dudu.service;import com.dudu.domain.LearnResource;import org.junit ...
- reids高可用(灾难备份-持久化)
java缓存存放到内存之中,当服务器重启以后,内存的数据将丢失,而reids作为缓存,重启reids以后 数据是不是也会丢失,redis服务器重启以后数据也不会丢失,这个是redis提供了持久化的功能 ...
- ubuntu 更换为mac主题
step1. 安装相关软件包 sudo add-apt-repository ppa:noobslab/themes sudo apt-get update sudo apt-get install ...
- SQL Server 2012无法连接到WMI提供程序
这篇文章主要介绍了SQL Server 2012无法连接到WMI提供程序(Cannot connect to WMI provider)解决方案,需要的朋友可以参考下 今天一位同事在启动自己工作机的S ...
- (转)SQLServer查询数据库各种历史记录
原文地址https://www.cnblogs.com/seusoftware/p/4826958.html 在SQL Server数据库中,从登陆开始,然后做了什么操作,以及数据库里发生了什么,大多 ...
- 分布式之redis复习精讲
看到一片不错的精简的redis文档,转载之,便于复习梳理之用 转自:https://www.cnblogs.com/rjzheng/p/9096228.html ------------------- ...