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