timus 1018. Binary Apple Tree
1018. Binary Apple Tree
Memory limit: 64 MB
|
Input
Output
Sample
input | output |
---|---|
|
|
题目大意: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 ...
随机推荐
- CAD制作简单动画
主要用到函数说明: IMxDrawEntity::Rotate 旋转一个对象.详细说明如下: 参数 说明 [in] IMxDrawPoint* basePoint 旋转基点 [in] DOUBLE d ...
- docker搭建日志收集系统EFK
EFK Elasticsearch是一个数据搜索引擎和分布式NoSQL数据库的组合,提过日志的存储和搜索功能. Fluentd是一个消息采集,转化,转发工具,目的是提供中心化的日志服务. Kibana ...
- 魂酥的NOIP2018(真实)游记
NOIP之后才开博客 作为一个高一零基础蒟蒻 想说什么似乎也没什么可说的 才学几个月似乎也没什么发言权就是了 Day -1 期中考爆0,似乎是班里学OI的考得最惨的一个 岂不美哉 要么我也没想考好 也 ...
- HTTP服务和APACHE
HTTP服务和APACHE 1. 跨Internet的主机间通讯 要通过Internet进行通信,至少需要一对套接字:其中一个运行在客户端,定义了一个唯一的客户进程,称之为ClientSocket,另 ...
- 3.3.3 char 类型
char类型原本用于表示单个字符.不过,现在情况已经有所变化.如今,有些Unicode字符可以用一个char值描述,另外一些Unicode字符则需要两个 char 值. char类 ...
- 用记事本写第一个Java程序
public class Welcome{ public static void main(String[] args){ System.out.println("我是尚学堂的高淇,很高兴认 ...
- 【12】AngularJS 事件
AngularJS 事件 AngularJS 有自己的 HTML 事件指令. ng-click 指令 ng-click 指令定义了 AngularJS 点击事件. <div ng-app=&qu ...
- Leetcode 80.删除重复数组的重复项
删除重复数组的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
- HDU 2082 母函数法
#include <cstdio> #include <cstring> using namespace std; ] , dp[][]; int main() { // fr ...
- VScode输出中文乱码的解决方法------测试过可以用
用python写个爬虫,配置个VScode环境,发现输出都是乱码,翻阅网站后发现一个简单有效的方法,在此谢过网络上的大牛们的无私分享,我也在此记录一下,以备后用: 文件---->首选项----& ...