URAL1018 Binary Apple Tree(树dp)
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对。
#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#define maxn 150
using namespace std; struct Edge
{
int v, w;
Edge(int vi, int wi) :v(vi), w(wi){}
Edge(){}
}; vector<Edge> G[maxn];
int n, q;
int dp[maxn][maxn];
int tmp[maxn]; void dfs(int u, int fa)
{
for (int i = 0; i < G[u].size(); i++){
int v = G[u][i].v, w = G[u][i].w;
if (v == fa) continue;
dfs(v, u);
memcpy(tmp, dp[u], sizeof(dp[u]));
for (int k = q; k >= 0; k--){
for (int j = k - 1; j >= 0; j--){
dp[u][k] = max(dp[u][k], dp[v][j] + tmp[k-1- j] + w);
}
}
}
} int main()
{
while (cin >> n >> q)
{
for (int i = 0; i <= n; i++) G[i].clear();
int ui, vi, wi;
for (int i = 0; i < n - 1; i++){
scanf("%d%d%d", &ui, &vi, &wi);
G[ui].push_back(Edge(vi, wi));
G[vi].push_back(Edge(ui, wi));
}
memset(dp, 0, sizeof(dp));
dfs(1, -1);
cout << dp[1][q] << endl;
}
return 0;
}
URAL1018 Binary Apple Tree(树dp)的更多相关文章
- Ural-1018 Binary Apple Tree(树形dp+分组背包)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...
- URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k ...
- URAL-1018 Binary Apple Tree---树形DP
题目链接: https://cn.vjudge.net/problem/URAL-1018 题目大意: 给你一棵树,每条边有一个边权,求以1为根节点,q条边的子数(q+1个点),边权和至最大. 解题思 ...
- URAL_1018 Binary Apple Tree 树形DP+背包
这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...
- URAL1018. Binary Apple Tree
链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...
- 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 ...
- CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划)
CJOJ 1976 二叉苹果树 / URAL 1018 Binary Apple Tree(树型动态规划) Description 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的 ...
- BNUOJ 13358 Binary Apple Tree
Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...
- timus 1018. Binary Apple Tree
1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...
随机推荐
- xcode plugin
http://alcatraz.io/ https://github.com/macoscope/CodePilot prepo curl -fsSL https://raw.githubuserc ...
- INFORMIX数据库常用命令
INFORMIX数据库常用命令 一.onstat命令集 1.onstat - 说明:查看数据库当前的状态 用法:onstat - 2.onstat -c 说明:查看数据库的配置文件 用法:ons ...
- android Init 相关分析
Init.c主要工作 1. 初始化属性(包括建立/dev./proc等目录.初始化属性.log.执行init.rc等初始化文件中的action等). 2. 解析配置文件的命令(主要是init.rc文件 ...
- net-snmp的安装
安装环境是ubuntu 14. 方法1:apt-get install net-snmp (非root用户需要sudo 提升权限) 方法2:自定义安装选择不同的版本去编译. 1:先去下载所需要的ta ...
- iOS学习之C语言分支结构
一.BOOL类型 返回值:真:YES 假:NO 定义一个布尔类型的变量 YES == 1, NO == 0 计算机在识别时,YES就替换成1,NO就替换成0 BOOL isGirl = YES; ...
- Linux 文件与目录
文件描述符 在内核中,所有打开的文件都使用文件描述符(一个非负整数)标记.文件描述符的变化范围是0~OPEN_MAX – 1.早期的unix系统中,每个进程最多可以同时打开20个文件,就是说文件描述符 ...
- 如何让webapi只返回json格式数据
最近脑子不好用,总记不住事,以前搞过让webapi只返回json格式的数据,今天有人问我又突然想不起了,后来总结一下,备忘一下,大概有下面几种处理方式 1.在WebApiConfig类的Registe ...
- SQLiteAPI函数详解
使用的过程根据使用的函数大致分为如下几个过程: sqlite3_open() sqlite3_prepare() sqlite3_step() sqlite3_column() sqlite3_fin ...
- 多线程 -- NSOperation
NSOperation 此类不能直接使用 NSInvocationOperation NSBlockOperation 定义一个类继承与它 NSInvocationOperation 可以使用star ...
- visualgo 数据结构与算法可视化工具
推荐可视化数据结构与算法工具 http://zh.visualgo.net/