URAL 1018 Binary Apple Tree(树DP)
|
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 ...
随机推荐
- Unix网络编程(迭代服务器)
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...
- [LeetCode]题解(python):031-Next Permutation
题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges nu ...
- select下拉框美化
其实用下列CSS就可以解决,原理是将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可. select { /*Chrome和Firefox里面的边框是不一样的,所以复 ...
- js 事件监听封装
var eventUtil={//添加句柄 //element,节点 //type,事件类型 //handler,函数 addHandler:function(element,type,handler ...
- C#反射代码
Object model=Assembly.Load(“程序集”).CreateInstance(命名空间.类名); object obj2 = Type.GetType("MyClass& ...
- saltstack之(九)配置管理源码部署Nginx
场景:rpm包安装的nginx服务,无法满足定制模块的需求,故线上环境使用nginx源码进行安装.本片文章详细介绍如何使用saltstack的配置管理功能实现nginx软件的源码安装. 下载源码:pc ...
- marathon参考(11):ports端口设置(转)
Ports marathon中应用的端口配置可能被混淆,并有一个悬而未决的问题,需要重新设计 ports API.这个页面试图更清楚地解释它们是如何工作的. 定义 containerPort:在容器内 ...
- mysql 授权 user@'%' 为什么登陆的时候localhost 不行呢???
公司业务服务器还没迁移到阿里云上的时候,创建的一个用户明明是所有的,但是本机登陆就是不行,一直也搞不懂原因 今天才知道 原来 %不包括 localhost mysql> grant all on ...
- 单臂路由+DHCP+VLAN
使用思科模拟软件Cisco Packet Tracer Student,软件功能有限,只能架设简单的网络架构,适合初学者使用.
- Oracle 10046 trace文件分析
生成10046 trace文件: SQL> create table t10046 as select * from dba_objects; Table created. SQL> se ...