ural 1018 Binary Apple Tree 树形dp
2013-10-30 20:03
337 查看
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 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 |
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. NextN − 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 |
n个节点,要求让你保留m条边,最大的价值。当然,有依赖的关系。
思路:保留是边,这个和其他题目,保留多少个点,就有区别了。
刚开始,题意都写错,第一种测试数据就错了。囧...
如何处理呢? 由于不知道,输入顺序中,谁是父亲节点,双向图+use[]标记就可以建树了。
状态转移也好写。
dp[ k ] [ j ] 以k为根节点,占用 j 个点获得的最大值。
dp[ k ] [ j ]= max( dp[ k ] [ j ], dp[ k ] [ j -s ] + dp [ t ] [ s ] );
那么就ok了么
不是的,题目意思是边,那你怎么办? 由于有依赖关系的存在,不可能存在单独的独立的几条线段,而是连接起来的
所以是m+1个节点。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; int n,m; struct node { int next[105]; int val[105]; int num; }f[105]; int dp[105][105]; bool use[105]; int Max(int x,int y) { return x>y? x:y; } void dfs(int k) { int i,j,t,s; use[k]=true; for(i=1;i<=f[k].num;i++) { t=f[k].next[i]; if(use[t]==true)continue; dp[t][1]=f[k].val[i]; dfs(t); for(j=m;j>=1;j--) { for(s=1;s<=j;s++) { if(dp[k][j-s]!=-1) dp[k][j]=Max(dp[k][j],dp[k][j-s]+dp[t][s]); } } } } int main() { int i,x,y,z; while(scanf("%d%d",&n,&m)>0) { memset(dp,-1,sizeof(dp)); memset(use,false,sizeof(use)); for(i=0;i<=100;i++) f[i].num=0; for(i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&z); f[x].num++; f[x].next[f[x].num]=y; f[x].val[f[x].num]=z; f[y].num++; f[y].next[f[y].num]=x; f[y].val[f[y].num]=z; } dp[1][1]=0; m++; dfs(1); printf("%d\n",dp[1][m]); } return 0; }
相关文章推荐
- Ural 1018 Binary Apple Tree(树形dp)
- URAL 1018 Binary Apple Tree(树形dp入门题)
- Ural 1018 Binary Apple Tree [树形dp]
- Ural 1018 Binary Apple Tree 树形dp
- ural 1018 Binary Apple Tree(树形DP)
- Ural 1018 Binary Apple Tree (树形dp)
- Ural 1018 Binary Apple Tree (树形DP)
- ural 1018 Binary Apple Tree(树形dp | 经典)
- ural 1018 Binary Apple Tree(树形DP)建二叉树
- URAL1018 Binary Apple Tree(树形DP)
- URAL 1018 Binary Apple Tree (树形DP)
- ural 1018 Binary Apple Tree(树形dp | 经典)
- URAL 1018 Binary Apple Tree(树形dp)
- URAL 1018 Binary Apple Tree (树形DP)
- 【URAL1018】【树形DP】Binary Apple Tree题解
- URAL 1018. Binary Apple Tree(树形DP)
- URAL 1018 Binary Apple Tree 简单树形背包
- ural 1018 Binary Apple Tree(树dp)
- URAL 1018 Binary Apple Tree(树DP)
- Binary Apple Tree_ural1018_树状dp