URAL1018 Binary Apple Tree(树形DP)
题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果。
挺简单的树形DP,因为是二叉树,都不需要树上背包什么的。
- dp[u][k]表示以u结点为根的子树保留k个分支最多能有的苹果数
- 转移就是左子树若干个,右子树若干个转移。。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 111
struct Edge{
int v,w,next;
}edge[MAXN<<];
int NE,head[MAXN];
void addEdge(int u,int v,int w){
edge[NE].v=v; edge[NE].w=w; edge[NE].next=head[u];
head[u]=NE++;
}
int n,q,d[MAXN][MAXN];
void dp(int u,int fa){
d[u][]=;
int lson=-,rson=-,w1,w2;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
if(lson==-) lson=v,w1=edge[i].w;
else rson=v,w2=edge[i].w;
dp(v,u);
}
if(lson==-) return;
if(rson==-){
for(int i=; i<q; ++i){
if(d[lson][i]==-) continue;
d[u][i+]=max(d[u][i+],d[lson][i]+w1);
}
return;
}
for(int i=; i<=q; ++i){
if(d[lson][i]==-) continue;
for(int j=; j<=q-i; ++j){
if(d[rson][j]==-) continue;
if(i+j+<=q) d[u][i+j+]=max(d[u][i+j+],d[lson][i]+d[rson][j]+w1+w2);
if(i== && j+<=q) d[u][j+]=max(d[u][j+],d[rson][j]+w2);
if(j== && i+<=q) d[u][i+]=max(d[u][i+],d[lson][i]+w1);
}
}
}
int main(){
int a,b,c;
while(~scanf("%d%d",&n,&q)){
NE=;
memset(head,-,sizeof(head));
for(int i=; i<n; ++i){
scanf("%d%d%d",&a,&b,&c);
addEdge(a,b,c);
addEdge(b,a,c);
}
memset(d,-,sizeof(d));
dp(,);
printf("%d\n",d[][q]);
}
return ;
}
URAL1018 Binary Apple Tree(树形DP)的更多相关文章
- 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个树枝的最大苹果数,但是在树形过 ...
- URAL1018 Binary Apple Tree(树dp)
组队赛的时候的一道题,那个时候想了一下感觉dp不怎么好写呀,现在写了出来,交上去过了,但是我觉得我还是应该WA的呀,因为总感觉dp的不对. #pragma warning(disable:4996) ...
- 【POJ 2486】 Apple Tree (树形DP)
Apple Tree Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to a ...
- poj 2486 Apple Tree(树形DP 状态方程有点难想)
Apple Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9808 Accepted: 3260 Descri ...
- POJ 2486 Apple Tree(树形DP)
题目链接 树形DP很弱啊,开始看题,觉得貌似挺简单的,然后发现貌似还可以往回走...然后就不知道怎么做了... 看看了题解http://www.cnblogs.com/wuyiqi/archive/2 ...
- URAL1018. Binary Apple Tree
链接 简单树形DP #include <iostream> #include<cstdio> #include<cstring> #include<algor ...
- URAL-1018 Binary Apple Tree---树形DP
题目链接: https://cn.vjudge.net/problem/URAL-1018 题目大意: 给你一棵树,每条边有一个边权,求以1为根节点,q条边的子数(q+1个点),边权和至最大. 解题思 ...
- POJ2486 - Apple Tree(树形DP)
题目大意 给定一棵n个结点的树,每个结点上有一定数量的苹果,你可以从结点1开始走k步(从某个结点走到相邻的结点算一步),经过的结点上的苹果都可以吃掉,问你最多能够吃到多少苹果? 题解 蛋疼的问题就是可 ...
随机推荐
- September 4th 2016 Week 37th Sunday
The morning crowned the humble cloud with splendor. 晨光为谦逊的白云披上壮丽的光彩. Humility is a virtue. Many famo ...
- printf()函数的参数和制表符
· 参数 · 控制符 · 转义序列 printf("这是第们学习的第4课"); printf("12345\n6789"); \n 换行 \r 回车键 \b ...
- android app 内部文件路径
public class MainActivity extends Activity { final String FILE_NAME = "crazyit.bin"; @Over ...
- WhaleSong
Chasingwaves by myself in theocean of endless sorrow Makingwishes that i will find myherd tomorrow 5 ...
- REORG TABLESPACE on z/os
这个困扰了我两天的问题终于解决了,在运行这个job时:总是提示 A REQUIRED DD CARD OR TEMPLATE IS MISSING NAME=SYSDISC A REQUIRED DD ...
- 两个viewport的故事(第一部分)
原文:http://www.quirksmode.org/mobile/viewports.html 在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如< ...
- 重温WCF之群聊天程序(十)
完成的效果图: 服务器端代码: using System; using System.Collections.Generic; using System.Linq; using System.Serv ...
- Android中libs目录下armeabi和armeabi-v7a的区别
armeabi默认选项,支持基于 ARM* v5TE 的设备支持软浮点运算(不支持硬件辅助的浮点计算)支持所有 ARM* 设备 armeabi-v7a支持基于 ARM* v7 的设备支持硬件 FPU ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- poj 1003:Hangover(水题,数学模拟)
Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 99450 Accepted: 48213 Descri ...