URAL 1018 Binary Apple Tree(树DP)
| 2 5 | 
Input
Output
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; int head[MAXN], ecnt;
int to[MAXN << ], next[MAXN << ], weight[MAXN << ];
int dp[MAXN][MAXN];
int n, m; void init() {
memset(head, -, sizeof(head));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; weight[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; weight[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} inline void update_max(int &a, int b) {
if(a < b) a = b;
} void dfs(int f, int u, int C) {
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f) continue;
for(int i = ; i <= C - ; ++i) dp[v][i] = dp[u][i] + weight[p];
dfs(u, v, C - );
for(int i = ; i <= C; ++i)
update_max(dp[u][i], dp[v][i - ]);
}
} int main() {
scanf("%d%d", &n, &m);
init();
for(int i = ; i < n; ++i) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
dfs(, , m);
printf("%d\n", dp[][m]);
}
URAL 1018 Binary Apple Tree(树DP)的更多相关文章
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
		CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ... 
- ural 1018 Binary Apple Tree(树形dp | 经典)
		本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ... 
- Ural 1018 Binary Apple Tree
		题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1018 Dynamic Programming. 首先要根据input建立树形结构,然后在 ... 
- URAL1018 Binary Apple Tree(树dp)
		组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ... 
- timus 1018. Binary Apple Tree
		1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ... 
- Ural-1018 Binary Apple Tree(树形dp+分组背包)
		#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ... 
- URAL_1018 Binary Apple Tree 树形DP+背包
		这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ... 
- BNUOJ 13358 Binary Apple Tree
		Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ... 
- POJ 3321 Apple Tree(树状数组)
		Apple Tree Time Limit: 2000MS Memory Lim ... 
随机推荐
- 查询mysql当前连接数
			标签: mysql服务器cachedisk 2012-08-23 23:06 23377人阅读 评论(0) 收藏 举报 分类: MySql(36) 1.show status Threads_co ... 
- hadoop与云技术、云计算混肴澄清
			本文引用自:http://www.aboutyun.com/blog-61-248.html 一.初学者问题: 请教个问题在实际的生成环境里面,数据源产生的地方部署Hadoop,还是需要程序把数据给迁 ... 
- Prism&MEF构建开发框架
			系统框架构想效果图 平台简单由左侧菜单和右侧内容区以及顶部系统和用户信息区构成 菜单根据系统模块动态加载 右侧,根据左侧选中菜单动态加载子模块,子模块集合以tab选项卡方式布局 系统模块划分为Shel ... 
- 利用快速排序原理找出数组中前n大的数
			#include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ... 
- 1011 最大公约数GCD
			1011 最大公约数GCD 基准时间限制:1 秒 空间限制:131072 KB 输入2个正整数A,B,求A与B的最大公约数. Input 2个数A,B,中间用空格隔开.(1<= A,B < ... 
- 玩儿了一下django User authentication
			五一在家,VPN不能链接了,而项目在本地run的过程中,又需要链接公司的SSO server才能login.下雨,不想去公司,又不得不在家做task,只能想办法避开SSO login,以前知道djan ... 
- python echo服务器和客户端(客户端可以用telnet之类的)
			发上来记录一下,省得下次再写一遍 服务器:server.py #-*- coding:utf-8 -*- from SocketServer import TCPServer, BaseRequest ... 
- Notepad++ install vi plugin
			下载Notepad++,想安装vi插件. 使用Notepad++自带的插件管理器下载visimulator失败. 所以直接下载插件visimulator.dll,再导入. 下载地址: https:// ... 
- 《JAVA NIO》第一章 简介
			1.2 CPU已不再是束缚 相反,是JVM 自身在I/O 方面效率欠佳.操作系统与Java 基于流的I/O模型有些不匹配. 操作系统要移动的是大块数据(缓冲区),这往往是在硬件直接存储器存取(DMA) ... 
- <dependency>spring-webmvc</dependency>
			Spring 4.2.0.RELEASE版本: <dependency> <groupId>org.springframework</groupId> <ar ... 
