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 ...
随机推荐
- Linux系统的启动流程
Linux系统的启动流程: 1.通电(通常按下电源键,开始通电) 2.加载BIOS (通常看到显示器提示按F2进入主板) 3.读取MBR (MBR硬盘的入口地址,用来装载引导) 4.进入引导 (通常有 ...
- Flask框架 之路由
一.视图函数路由规则 from flask import Flask, redirect, url_for # 创建flask应用对象 # __name__ 代表当前模块名称 # flask以当前目录 ...
- Vue指令4:v-on
监听事件 事件:click\keydown <button v-on:click="greet"></button> 可以简写为 <button @ ...
- 100 道 Linux 笔试题,能拿 80 分就算大神!
本套笔试题共100题,每题1分,共100分.(参考答案在文章末尾) 1. cron 后台常驻程序 (daemon) 用于: A. 负责文件在网络中的共享 B. 管理打印子系统C. 跟踪管理系统信息和错 ...
- c# Dictionary 扩展方法
主要用于接口请求,数据转换 #region Dictionary 扩展方法 public static string getString(this Dictionary<string, stri ...
- [Git]Please make sure you have the correct access rights and the repository exists
这个问题是这样,需要在已有github账号的A机器上,再创建一个github账号,新账号创建完毕,将代码通过机器A push上之后,再另一台机器B,clone 这个项目时报出了如下错误: Permis ...
- ORACLE 查看当前用户信息(用户,表视图,索引,表空间,同义词,存储过程,约束条件)
1.用户 查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select ...
- Java核心技术 卷一 复习笔记(乙
1.字符串从概念上讲,Java字符串就是Unicode字符序列.Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义类,叫String. 每个用双引号括起来的字符串都是 String ...
- Discuz 部署,500 – 内部服务器错误。 您查找的资源存在问题,因而无法显示。
Windows7 IIS 500 – 内部服务器错误解决方案 1.解决方法:打开IIS,在功能视图中找到“错误页”,双击进去后,看最右边的“操作”下的“编辑功能设置…”,将“错误响应”下的“详细错误” ...
- Entity SQL rules for Wrapped and Unwrapped Results
Here are some rules to remember for Entity SQL queries: 1.Use SELECT VALUE when projecting more than ...