zoukankan      html  css  js  c++  java
  • 【树形dp】Rebuilding Roads

    [POJ1947]Rebuilding Roads
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 11934   Accepted: 5519

    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.] 

    Source

     
    题目大意:有一颗N个节点的树,问最少删去几条边使剩下的树的大小有一颗为P?
    直接写不就好了么?
    代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    
    inline int read(){
    	int x=0,f=1;char c=getchar();
    	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    	return x*f;
    }
    const int MAXN=100001;
    const int INF=999999;
    int N,M;
    vector<int> vec[201];
    int dp[201][201];
    int ans=INF;
    
    void dfs(int x,int fa){
    	int cnt=0;
    	for(int i=0;i<vec[x].size();i++){
    		if(vec[x][i]!=fa)
    			dfs(vec[x][i],x),cnt++;
    	}
    	dp[x][1]=dp[x][0]=0;
    	for(int i=0;i<vec[x].size();i++){
    		if(vec[x][i]!=fa)
    		for(int j=M;j>=1;j--){
    			if(dp[x][j]!=INF) dp[x][j]++;
    			for(int k=1;k<=M;k++){
    			    if(k>=j||dp[vec[x][i]][k]==INF) break;
    			    if(dp[x][j-k]!=INF) dp[x][j]=min(dp[vec[x][i]][k]+dp[x][j-k],dp[x][j]);
    			}
    		}
    	}
    	if(x!=1) ans=min(ans,dp[x][M]+1);
    	else ans=min(ans,dp[x][M]);
    	return ;
    }
    
    int main(){
    	N=read(),M=read();
        for(int i=0;i<=N;i++)
    	    for(int j=0;j<=M;j++) dp[i][j]=INF;
    	for(int i=1;i<N;i++){
    		int u=read(),v=read();
    		vec[u].push_back(v);
    		vec[v].push_back(u);
    	}
    	dp[1][1]=0;
    	dfs(1,-1);
    	if(ans!=INF) printf("%d
    ",ans);
    	else puts("0");
    }
    //dp[i][j]表示i号节点的子树中隔离成为大小为j个的道路数量
    //dp[i][k]=min(dp[i->son][j]+dp[i][k-j]) 
  • 相关阅读:
    expect详解及自动登录脚本的实现
    NFS服务端+客户端配置
    交互式shell脚本对话框----whiptail指令
    自定制Centos7.3系统镜像(ISO)
    云原生简述
    Linux下修改MTU(最大传输单元)
    MySQL-5.7组提交(Group Commit)原理
    AWS putty终端ssh密钥登陆方法
    一个简单的从web页面获取数据插入数据库的小程序
    新建一个简单的Java web前后台交互小程序时报错:java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SqlServerDriver
  • 原文地址:https://www.cnblogs.com/wxjor/p/7270908.html
Copyright © 2011-2022 走看看