多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > poj1947--Rebuilding Roads(樹狀dp)

poj1947--Rebuilding Roads(樹狀dp)

來源:程序員人生   發布時間:2015-03-25 11:56:46 閱讀次數:3444次
Rebuilding Roads
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

給出n個節點的樹,給出值m。問最少刪除幾條邊可以得到節點個數為m的子樹。

樹狀dp,統計出以節點i為根的子樹得到節點個數為j的子樹最少刪除的邊數。

 

#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define INF 0x3f3f3f3f struct node { int v , next ; }edge[160] ; int head[160] , cnt ; int c[160][160] , sum[160]; void add(int u,int v) { edge[cnt].v = v ; edge[cnt].next = head[u] ; head[u] = cnt++ ; } void dfs(int u) { sum[u] = 1 ; if( head[u] == ⑴ ) { 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(head,⑴,sizeof(head)) ; memset(c,INF,sizeof(c)) ; memset(sum,0,sizeof(sum)) ; cnt = 0 ; scanf("%d %d", &n, &p) ; add(0,1) ; for(i = 0 ; i < n⑴ ; i++) { scanf("%d %d", &u, &v) ; add(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 ; }


 

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 色妞影视 | 黄网址大全免费观看免费 | 福利网在线 | 波多野结衣在线观看一区二区 | 欧美xxxx三人交性视频 | 亚洲涩福利高清在线 | 福利视频一区二区微拍堂 | 乱人伦中文视频在线 | 日本乱妇 | 亚洲高清免费 | 一级毛片aa| 国产一区二区三区在线免费观看 | 一级特黄aa大片免费播放视频 | babes性欧美30 | 欧美视频福利 | 精品国产综合区久久久久久 | 夜夜操狠狠干 | 日日撸夜夜操 | 最近最中文字幕视频 | 一道本一区二区三区 | 亚洲欧美日韩精品久久久 | 国产午夜免费一区二区三区 | 自拍偷自拍亚洲精品情侣 | 中文国产成人精品久久一区 | 宇都宫紫苑野外中文字幕 | 美毛片| 无人区理论片手机看片 | 欧美成人黑人xx视频免费观看 | 午夜精品久久久久久中宇 | 日本爽| 日韩欧美成末人一区二区三区 | 夜夜躁日日躁 | 久久一本一区二区三区 | 一区二区三区在线视频观看 | 意大利极品xxxxhd | 精品久久看 | 免费亚洲一区 | 可以免费观看的黄色网址 | xxx性欧美在线观看 xxx性日本 | 成年人免费视频网站 | 亚洲成在线 |