Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9496 Accepted: 4316

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⑴ 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⑷ and 1⑸ are destroyed.]

Source

USACO 2002 February

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define INF 0x3f3f3f3f
struct node {
int v , next ;
}edge[160] ;
int c[160][160] , sum[160];
edge[cnt].v = v ;
}
void dfs(int u)
{
sum[u] = 1 ;
{
c[u][ sum[u] ] = 0 ;
return ;
}
int i , j , k , v , temp ;
for(i = head[u] ; i != ⑴ ; i = edge[i].next) {
v = edge[i].v ;
dfs(v) ;
sum[u] += sum[v] ;
}
c[u][ sum[u] ] = 0 ;
for(i = head[u] ; i != ⑴ ; i = edge[i].next) {
v = edge[i].v ;
c[v][0] = 1 ;
for(j = 0 ; j <= sum[u] ; j++) {
for(k = 0 ; k <= sum[v] ; k++) {
temp = sum[v] – k ;
if( j >= temp )
c[u][ j-temp ] = min( c[u][j-temp],c[u][j]+c[v][k] ) ;
}
}
c[v][0] = INF ;
}
return ;
}
int main() {
int n , p , i , u , v ;
memset(c,INF,sizeof(c)) ;
memset(sum,0,sizeof(sum)) ;
cnt = 0 ;
scanf("%d %d", &n, &p) ;
for(i = 0 ; i < n⑴ ; i++) {
scanf("%d %d", &u, &v) ;
}
dfs(0) ;
int min1 = c[1][p] ;
for(i = 2 ; i <= n ; i++) {
min1 = min(min1,c[i][p]+1) ;
}
printf("%d
", min1) ;
return 0 ;
}

1. 本站所有资源来源于用户上传和网络，如有侵权请邮件联系站长！
2. 分享目的仅供大家学习和交流，您必须在下载后24小时内删除！
3. 不得使用于非法商业用途，不得违反国家法律。否则后果自负！
4. 本站提供的源码、模板、插件等等其他资源，都不包含技术服务请大家谅解！
5. 如有链接无法下载、失效或广告，请联系管理员处理！
6. 本站资源售价只是赞助，收取费用仅维持本站的日常运营所需！
7. 本站源码并不保证全部能正常使用，仅供有技术基础的人学习研究，请谨慎下载
8. 如遇到加密压缩包，请使用WINRAR解压,如遇到无法解压的请联系管理员！