poj 1947 Rebuilding Roads解题报告
2015-04-11 20:05
330 查看
题目点我
Rebuilding Roads
Description
The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get
from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
Sample Input
Sample Output
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.] Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9582 Accepted: 4364
Description
The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other
barn. Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
Sample Input
11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11
Sample Output
2
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]
题目大意:通过减边得到给出结点数的子树。要注意的是并不是所有的case中1都是根,所以我们要找到根。题中给出的是有根树。
第一道树形dp,强行理解了好久。
首先建立一个树,dfs用后序遍历。
对于任意一个结点,dp[I][j]代表从结点I分离出j个结点的最少减边数。
每个点都有两种状态转移,
1.这个点包括在剩下的子树中。dp[I][j]=dp[I][j]+1
2.这个点不包括在剩下的子树中。dp[I][j]=dp[I][j-m]+dp[k][m] k为i的子树。
这个dp计算应该叫刷表法吧?
一些测试数据可以在poj的讨论版里找到。
题目点我
Rebuilding Roads
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 9582 | Accepted: 4364 |
The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get
from any given barn to any other barn. Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
Sample Input
11 6 1 2 1 3 1 4 1 5 2 6 2 7 2 8 4 9 4 10 4 11
Sample Output
2
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.] Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9582 Accepted: 4364
Description
The cows have reconstructed Farmer John's farm, with its N barns (1 <= N <= 150, number 1..N) after the terrible earthquake last May. The cows didn't have time to rebuild any extra roads, so now there is exactly one way to get from any given barn to any other
barn. Thus, the farm transportation system can be represented as a tree.
Farmer John wants to know how much damage another earthquake could do. He wants to know the minimum number of roads whose destruction would isolate a subtree of exactly P (1 <= P <= N) barns from the rest of the barns.
Input
* Line 1: Two integers, N and P
* Lines 2..N: N-1 lines, each with two integers I and J. Node I is node J's parent in the tree of roads.
Output
A single line containing the integer that is the minimum number of roads that need to be destroyed for a subtree of P nodes to be isolated.
Sample Input
11 6
1 2
1 3
1 4
1 5
2 6
2 7
2 8
4 9
4 10
4 11
Sample Output
2
Hint
[A subtree with nodes (1, 2, 3, 6, 7, 8) will become isolated if roads 1-4 and 1-5 are destroyed.]
题目大意:通过减边得到给出结点数的子树。要注意的是并不是所有的case中1都是根,所以我们要找到根。题中给出的是有根树。
第一道树形dp,强行理解了好久。
首先建立一个树,dfs用后序遍历。
对于任意一个结点,dp[I][j]代表从结点I分离出j个结点的最少减边数。
每个点都有两种状态转移,
1.这个点包括在剩下的子树中。dp[I][j]=dp[I][j]+1
2.这个点不包括在剩下的子树中。dp[I][j]=dp[I][j-m]+dp[k][m] k为i的子树。
这个dp计算应该叫刷表法吧?
#include<stdio.h> #include<string.h> #include<algorithm> #include<vector> using namespace std; int father[157]; int root; int dp[157][157]; int n, p; struct Node //结点 { vector<int> son; }node[157]; //typedef struct node; void draw(int x, int y) //建立树 { node[x].son.push_back(y); } void init() { memset(father, 0, sizeof(father)); for(int i=1;i<157;i++){ node[i].son.clear(); } } void dfs(int root) //对于任意一个点,我们要先计算他所有的子树,因此用后序遍历 { int len=node[root].son.size(); for(int i=0;i<=p;i++){ dp[root][i]=157; //这个数要很大(至少比能在计算中取到的要大),这个数表示这个状态不成立(计算的时候min就可以把他舍去) } dp[root][1]=0; if(len==0) return ; for(int i=0;i<len;i++){ dfs(node[root].son[i]); } for(int j=0;j<len;j++){ int k=node[root].son[j]; for(int l=p;l>=1;l--){ int t=dp[root][l]+1; for(int m=1;m<l;m++){ t=min(t, dp[root][l-m]+dp[k][m]); } dp[root][l]=t; } } return ; } void solve() { int ans; dfs(root); ans=dp[root][p]; for(int i=1;i<=n;i++){ //如果答案不在根结点上要多减一条边 ans=min(ans, dp[i][p]+1); } printf("%d\n", ans); return ; } int main() { while(scanf("%d%d", &n, &p)!=EOF&&(n!=0||p!=0)){ init(); for(int i=1;i<n;i++){ int x, y; scanf("%d%d", &x, &y); if(x>y) swap(x, y); father[y]++; draw(x, y); } for(int i=1;i<=n;i++){ //没有父亲的点是根 if(!father[i]) { root=i; } } solve(); } }
一些测试数据可以在poj的讨论版里找到。
相关文章推荐
- POJ1207解题报告----极水的题目
- 【POJ 1025 Department 解题报告】
- 【原】 POJ 1050 To the Max 求二维矩阵的最大子矩阵 解题报告
- 【原】 POJ 2159 Ancient Cipher 题意万岁 解题报告
- 【原】 POJ 3067 Japan 2D树状数组+逆序数 解题报告
- poj 2449 Remmarguts' Date 第k短路 A*+spfa 解题报告
- POJ - 2481 Cows解题报告(树状数组 相同点重复计数处理)
- POJ 2744 子串 解题报告
- poj2914解题报告
- POJ-1308 & HDOJ-1325 Is It A Tree? 解题报告
- POJ_1007:DNA Sorting解题报告
- POJ专项——解题报告
- poj解题报告——1970
- POJ 2974 487-3279 解题报告
- POJ 1011 Sticks 解题报告
- POJ 2811 熄灯问题 解题报告
- poj 1046 Color Me Less 解题报告
- 解题报告 poj 1011 木棒
- POJ 2818 密码 解题报告
- POJ 3176 解题报告