树形 dp
// ACM训练联盟周赛 C. Teemo's tree problem
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define N 209
using namespace std;
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
int t,n,q;
int u,v,w,head[N],val[N];
int num[N];//num[i]: 包含i在内及其后面所有点的个数
int dp[N][N]; //dp[i][j] :以i为“根”,取j个点可获的最大价值
struct Edge{
int from,to,nex,w;
}e[N];
int cnt;
void init()
{
mem(head,-);//-1,因为cnt初始化为了0
mem(val,);
mem(dp,);
mem(num,);
cnt=;
}
void add(int u,int v,int w){
e[cnt].from=u;
e[cnt].to=v;
e[cnt].w=w;
e[cnt].nex=head[u];
head[u]=cnt++;
}
void getval(int u){
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(val[v]==){//找过的不会在找了
val[v]=e[i].w;//除根节点外把边的值赋给点
getval(v);
}
}
}
int dfs(int u,int fa)
{
num[u]=;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(v==fa) continue;
num[u]+=dfs(v,u);
}
dp[u][]=val[u];//只有自己,因此val[1]==0 :去掉所有的边才会只有1
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].to;
if(v==fa) continue;
for(int j=num[u];j>=;j--){//逆序,若正序比如 j==1 ,无法更新,后面的也会出现错误
for(int k=;k<j&&k<=num[v];k++){
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
}
}
}
return num[u];
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&q);
for(int i=;i<n-;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);//无向图
}
val[]=inf;//不是0就可以
getval();
val[]=;
dfs(,);
printf("%d\n",dp[][q+]);//q条边对应q+1个点
}
return ;
}
树形 dp的更多相关文章
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
- POJ2342 树形dp
原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...
- hdu1561 The more, The Better (树形dp+背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...
- bzoj2500: 幸福的道路(树形dp+单调队列)
好题.. 先找出每个节点的树上最长路 由树形DP完成 节点x,设其最长路的子节点为y 对于y的最长路,有向上和向下两种情况: down:y向子节点的最长路g[y][0] up:x的次长路的g[x][1 ...
- BZOJ 1040 树形DP+环套树
就是有n个点n条边,那么有且只有一个环那么用Dfs把在环上的两个点找到.然后拆开,从这条个点分别作树形Dp即可. #include <cstdio> #include <cstrin ...
随机推荐
- 洛谷 P2376 [USACO09OCT]津贴Allowance
https://www.luogu.org/problemnew/show/P2376 看了题解做的,根本不会贪心.. #include<cstdio> #include<algor ...
- 问题: 查看某个文件的修改记录| git log 高级用法
参考文章: git查看某个文件的修改历史 5.3 Git log 高级用法 基本步骤 git log --pretty=oneline [文件名] git show [节点] git log 两周高级 ...
- Ubuntu设置右键打开终端
1:设置Ubuntu右键打开终端. Ctrl+Alt+T 打开终端 $ sudo apt-get ins tall nautilus-open-terminal 重启系统 2:进入root用户认证失败 ...
- 使用nvmw解决windows下多版本node共存的问题
不支持4.x的nodejs,用nodist吧 不支持4.x的nodejs,用nodist吧 不支持4.x的nodejs,用nodist吧 ===========不要再看的分割线============ ...
- Unity Shader入门精要学习笔记 - 第11章 让画面动起来
转自 冯乐乐的 <Unity Shader入门精要> Unity Shader 中的内置变量 动画效果往往都是把时间添加到一些变量的计算中,以便在时间变化时画面也可以随之变化.Unity ...
- django modelform中的self.instance
在stackoverflow上看到一个问题,正好是我疑惑很久的相关问题. [原问题地址]https://stackoverflow.com/questions/18265023/self-instan ...
- P1816 忠诚 倍增
链接:https://www.luogu.org/problem/show?pid=1816 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k ...
- 极简的css浮动窗口
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- VBA小记
要放假了,可是我们,我还是煎熬! 最让人不爽的是媳妇也需要加班加点的完成一些看起来很EASY的事: 统计数据,把几个表合并…… EXCEL本人还是懂得一点点的(我不想说我是学计算机的,我怕给学计算机的 ...
- css制作三分圆形
效果图展示: 原理很简单,主要运用transform这个样式,通过斜切和旋转达成 html: css: 怎样,是不是很简单