timus 1018. Binary Apple Tree
1018. Binary Apple Tree
Memory limit: 64 MB
2 5 |
Input
Output
Sample
input | output |
---|---|
5 2 |
21 |
题目大意:n个点 n-1条边,现在要保留Q条边,求保留下的边的去权值和的最大值。
把边的权值映射到点上,边的权值相当于这个点指向根节点的权值,所以问题转换成对点的操作。
先统计出以当前点为根节点的子树的点数(包括当前根节点),然后dp,
这里dp,以u为根节点保留j个点能得到最大值,状态转移方程
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v]k]+val)
val是v到u的权值。
/* ***********************************************
Author :guanjun
Created Time :2016/10/15 15:43:48
File Name :timus1018.cpp
************************************************ */
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
struct node{
int y;
int val;
}; vector<node>v[];
int sz[],n,m,num;
int dp[][];
void dfs(int u,int fa){
num++;
sz[u]=;
for(int i=;i<v[u].size();i++){
int y=v[u][i].y;
if(y==fa)continue;
dfs(y,u);
sz[u]+=sz[y];
}
}
void dfs2(int u,int fa){
for(int i=;i<v[u].size();i++){
int y=v[u][i].y;
int val=v[u][i].val;
if(y==fa)continue;
//cout<<u<<" "<<sz[u]<<endl;
dfs2(y,u);
for(int j=sz[u];j>;j--){
for(int k=;k<j;k++){
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[y][k]+val);
}
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
num=;
int x,y,z;
cle(sz);
for(int i=;i<n;i++){
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
cle(dp);
dfs(,-);
dfs2(,-);
cout<<dp[][m+]<<endl;
}
return ;
}
timus 1018. Binary Apple Tree的更多相关文章
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- URAL 1018 Binary Apple Tree(树DP)
Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a bina ...
- Ural 1018 Binary Apple Tree
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在 ...
- ural 1018 Binary Apple Tree(树形dp | 经典)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- 【URAL 1018】Binary Apple Tree
http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...
- URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...
- URAL1018 Binary Apple Tree(树dp)
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...
- URAL1018. Binary Apple Tree
链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...
随机推荐
- (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
http://blog.csdn.net/yerenyuan_pku/article/details/72863323 我们知道Jedis在处理Redis的单机版和集群版时是完全不同的,有可能在开发的 ...
- 安卓app测试之内存分析
一.内存分析步骤 1.启动App. 2.使用monitor命令打开:ADM(包含DDMS) ->update heap 3.操作app,点几次GC 4.dump heap 5.hprof-con ...
- jmeter的webservice接口测试(SOAP/XML-RPC Request)
摘自:https://www.cnblogs.com/imyalost/p/5804359.html 项目背景:公司的微信端H5界面新开发了会员注册功能,需要对其进行压力测试 项目目标:需要承受每分钟 ...
- Spring Boot 2.0的属性绑定
Spring Boot2.0的属性绑定 原文从Spring boot第一个版本以来,我们可以使用@ConfigurationProperties注解将属性绑定到对象.也可以指定属性的各种不同格式.比如 ...
- spring cloud (一):大话 Spring Cloud
转自:http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spr ...
- Python学习:ModuleNotFoundError: No module named 'pygal.i18n' 的解决方法
最近在学<Python编程:从入门到实践>,16.2小结中 from pygal.i18n import COUNTRIES 获取两个字母的国别码,我用的pygal的版本是2.4.0(终端 ...
- SVM简单上手示例
转载自 百度知道 id:风_南(https://zhidao.baidu.com/usercenter?uid=e9904069236f25705e799313) 转载只为方便学习复习,侵删. 在用s ...
- 腾讯云:iptables基础
iptables 基础 iptables 基本命令 任务时间:5min ~ 10min iptables 可以简单理解为 Linux 系统内核级防火墙 netfilter 的用户态客户端. Linux ...
- Spring AOP学习(六)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 【16】AngularJS API
AngularJS API API 意为 Application Programming Interface(应用程序编程接口). AngularJS 全局 API AngularJS 全局 API ...