URAL 1018 Binary Apple Tree(樹形dp入門題)
URAL上簡單的樹形dp,但是都錯了題意。。。sad,是保留m個樹幹,不是刪掉m個樹幹。求保留的最多的蘋果有多少。根據樹的特點,每個dp[i][j]表示第i個節點保留了j個樹幹。dp[i][j] = max(dp[i][j], dp[x1][k-1]+map[x1][i] + dp[x2][j-k-1] + map1[x2][i]).就是多的這個節點是屬於哪個子樹上的。
1018. Binary Apple Tree
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Let's imagine how apple tree looks in binary computer world. You're right, it looks just like a binary tree, i.e. any biparous branch splits up to exactly two new branches. We will enumerate by integers the root of binary apple
tree, points of branching and the ends of twigs. This way we may distinguish different branches by their ending points. We will assume that root of tree always is numbered by 1 and all numbers used for enumerating are numbered in range from 1 to
N, where N is the total number of all enumerated points. For instance in the picture below
N is equal to 5. Here is an example of an enumerated tree with four branches:
2 5 \ / 3 4 \ / 1 |
As you may know it's not convenient to pick an apples from a tree when there are too much of branches. That's why some of them should be removed from a tree. But you are interested in removing branches in the way of minimal loss
of apples.So your are given amounts of apples on a branches and amount of branches that should be preserved. Your task is to determine how many apples can remain on a tree after removing of excessive branches.
Input
First line of input contains two numbers: N and
Q (2 ≤ N ≤ 100;1 ≤ Q ≤ N − 1).
N denotes the number of enumerated points in a tree. Q denotes amount of branches that should be preserved. Next
N − 1 lines contains descriptions of branches. Each description consists of a three integer numbers divided by spaces. The first two of them define branch by it's ending points. The third number defines the number of apples on this branch. You may
assume that no branch contains more than 30000 apples.
Output
Output should contain the only number — amount of apples that can be preserved. And don't forget to preserve tree's root ;-)
Sample
input | output |
---|---|
5 2 1 3 1 1 4 10 2 3 20 3 5 20 |
21 |
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 110;
using namespace std;
vector<int>g[maxn];
vector<int>now[maxn];
int dp[maxn][maxn];
int vis[maxn];
int map1[maxn][maxn];
int n, m;
void change(int x)
{
vis[x] = 1;
for(int i = 0; i < g[x].size(); i++)
{
if(vis[g[x][i]])
continue;
now[x].push_back(g[x][i]);
change(g[x][i]);
}
}
void dfs(int x)
{
dp[x][0] = 0;
for(int i = 0; i < now[x].size(); i++)
dfs(now[x][i]);
if(now[x].size() == 0)
return;
int ll = now[x][0];
int rr = now[x][1];
for(int i = 1; i <= m; i++)
{
for(int j = 0; j <= i; j++)
{
int xx = 0;
if(j != 0)
xx += (dp[ll][j-1]+map1[x][ll]);
if(i-j != 0)
xx += (dp[rr][i-j-1]+map1[x][rr]);
dp[x][i] = max(dp[x][i],xx);
}
}
}
int main()
{
while(cin >>n>>m)
{
for(int i = 0; i <= n; i++)
{
now[i].clear();
g[i].clear();
map1[i][i] = 0;
}
memset(dp, 0 , sizeof(dp));
memset(vis, 0 , sizeof(vis));
int u, v, w;
for(int i = 0; i < n-1; i++)
{
cin >>u>>v>>w;
g[u].push_back(v);
g[v].push_back(u);
map1[u][v] = w;
map1[v][u] = w;
}
change(1);
dfs(1);
cout<<dp[1][m]<<endl;
}
}
相關文章
- POJ 2486 Apple Tree(樹形dp)APP
- Codeforces 461B. Appleman and Tree[樹形DP 方案數]APP
- poj2486Apple Tree[樹形揹包!!!]APP
- 樹形DP
- 樹形DP!
- Codeforces 461B Appleman and Tree:Tree dpAPP
- 樹上染色(樹形dp)
- 線段樹入門(Segment Tree)
- [筆記]樹形dp筆記
- CF 1029E Tree with Small Distances 樹形DP or 貪心
- 概率DP入門題
- 樹形DP二三知識
- CF1039D You Are Given a Tree (樹形 dp + 貪心 + 根號分治)
- Maximum Depth of Binary Tree 二叉樹的深度二叉樹
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉樹二叉樹
- LayUI—tree樹形結構的使用UI
- 【DP】區間DP入門
- php tree類的使用(樹形結構)PHP
- CCF之網路延時(樹形dp)
- POJ 3107 Godfather(樹形dp)Go
- HDU 5326 Work (基礎樹形dp)
- hdu 4123 樹形DP+RMQMQ
- 高效能Mysql 入門到放棄 之 B+-Tree (與B-Tree以及Binary Tree的對比解析)MySql
- POJ3321Apple Tree[樹轉序列 BIT]APP
- POJ 3321-Apple Tree(樹狀陣列)APP陣列
- 二叉搜尋樹(Binary Search Tree)(Java實現)Java
- URAL 1658. Sum of Digits(簡單dp)Git
- POJ3107Godfather[樹形DP 樹的重心]Go
- 8 個最好的 jQuery 樹形 Tree 外掛jQuery
- 28款jQuery Tree 樹形結構外掛jQuery
- 【動態規劃】樹形DP完全詳解!動態規劃
- 【樹形dp】poj 1947 Rebuilding RoadsRebuild
- POJ 3321 Apple Tree(dfs+樹狀陣列)APP陣列
- 03【若依框架解讀】Tree樹形結構的控制(選單,部門)框架
- ZROJ#398. 【18提高7】隨機遊走(期望dp 樹形dp)隨機
- 257. Binary Tree Paths(列印二叉樹所有路徑)二叉樹
- 一個 vue 樹形外掛 vue-simple-treeVue
- POJ3321 Apple Tree(DFS序 + 樹狀陣列)APP陣列