BNUOJ 13358 Binary Apple Tree

Binary Apple Tree

Time Limit: 1000ms

Memory Limit: 16384KB

This problem will be judged on Ural. Original ID: 1018
64-bit integer IO format: %lld      Java class name: (Any)

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 Nis 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. Qdenotes 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

5 2
1 3 1
1 4 10
2 3 20
3 5 20

Sample Output

21

Source

Ural State University Internal Contest ‘99 #2

解题:树形dp,各种dp各种凌乱。dp[u][i]表示标号为u的根,保留i个条树枝。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 struct arc {
18     int to,w;
19     arc(int x = 0,int y = 0):to(x),w(y) {}
20 };
21 vector<arc>g[110];
22 int n,m,dp[110][110],cnt[110];
23 void dfs(int u,int fa) {
24     cnt[u] = 1;
25     for(int i = 0; i < g[u].size(); i++) {
26         if(g[u][i].to == fa) continue;
27         dfs(g[u][i].to,u);
28         cnt[u] += cnt[g[u][i].to];
29     }
30     for(int i = 0; i < g[u].size(); i++) {
31         if(g[u][i].to == fa) continue;
32         for(int j = cnt[u]; j > 0; j--) {
33             for(int k = 1; k <= cnt[g[u][i].to] && k < j; k++) {
34                 dp[u][j] = max(dp[u][j],dp[u][j-k]+dp[g[u][i].to][k]+g[u][i].w);
35             }
36         }
37     }
38 }
39 int main() {
40     int i,u,v,w;
41     while(~scanf("%d %d",&n,&m)) {
42         for(i = 0; i <= n; i++)
43             g[i].clear();
44         for(i = 1; i < n; i++) {
45             scanf("%d %d %d",&u,&v,&w);
46             g[u].push_back(arc(v,w));
47             g[v].push_back(arc(u,w));
48         }
49         memset(dp,0,sizeof(dp));
50         dfs(1,-1);
51         printf("%d\n",dp[1][m+1]);
52     }
53     return 0;
54 }

BNUOJ 13358 Binary Apple Tree

时间: 2024-10-09 15:06:58

BNUOJ 13358 Binary Apple Tree的相关文章

timus 1018. Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory 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 enu

ural 1018 Binary Apple Tree

1018. Binary Apple Tree Time limit: 1.0 secondMemory 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 enu

URAL1018 Binary Apple Tree

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description 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 e

URAL_1018 Binary Apple Tree 树形DP+背包

这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过程中,有点难表示转移 后来看了下大神的做法才知道其实可以用背包来模拟 树枝的去留,其实真的是个背包诶,每个子树枝就相当于物品,他占用了多少树枝量,带来多少的收益,就是用背包嘛,于是用树形DP+背包就可以做了 #include <iostream> #include <cstdio> #

URAL 1018 Binary Apple Tree 树形DP 好题 经典

1018. Binary Apple Tree Time limit: 1.0 secondMemory 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 enu

URAL1018 Binary Apple Tree(树形DP)

题目大概说一棵n结点二叉苹果树,n-1个分支,每个分支各有苹果,1是根,要删掉若干个分支,保留q个分支,问最多能保留几个苹果. 挺简单的树形DP,因为是二叉树,都不需要树上背包什么的. dp[u][k]表示以u结点为根的子树保留k个分支最多能有的苹果数 转移就是左子树若干个,右子树若干个转移.. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 #d

Ural 1018 binary apple tree(显性树的树dp)

题意:一棵含n个节点的树,保留m条边,使含m条边的子树的边权和最大: 思路:树dp.求含m+1个节点边权和最大的子树.对每个分支节点有三种操作:剪去左子树:剪去右子树:将其节点数合理分配给左右子树: 记以x为根,含k个节点的子树的最大边权和为g[x][k]. 若x为叶节点,则g[x][k]为x通往父节点的边权:若非叶节点,枚举k-1个节点分配在左右子树的所有可能方案,找到最佳方案: #include<cstdio> #include<cstring> #include<alg

【树状数组】POJ 3321 Apple Tree

/** * @author johnsondu * @time 2015.8.25 20:04 * @problem POJ 3321 Apple Tree * @type Binary Index Tree * @description 从根节点开始,dfs遍历树,先访问的节点 * 记为beg, 从当前结点遍历访问到的最后的 * 一个节点,记为end.然后按照树状数组的 * 方法进行求解. * @url http://poj.org/problem?id=3321 */ #include <i

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod