poj 1947 Rebuilding Roads 树形dp 删边类树形dp

Rebuilding Roads
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 10070 Accepted: 4598

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的树,问至少砍几刀可以孤立出一棵节点为m的子树。

2016/10/25:
最新代码:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= 150+5   ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
// by yskysker123
int n,p,root;
vector<int> son[maxn];
bool vis[maxn];
int dp[maxn][maxn];//dp[i][j]表示达到根节点为i,节点数为j的状态,需要的操作次数

void dfs(int x,int fa)
{

   int siz=son[x].size();
   memset(dp[x],0x3f, (n+1)*sizeof dp[x][0]);
   dp[x][0]=1;//x和父节点断开,此时dp[x][v>1]状态不可大达
   dp[x][1]=0;

    for(int i=0;i<siz;i++)//首先考虑没有子节点的情况,然后子节点一个一个考虑进去
    {
        int y=son[x][i]; if(y==fa)  continue;

        dfs(y,x);

        for(int all=p;all>=1;all--)
        {
              dp[x][all]++;     //完全不要新子树      //新加一个节点,如果x为根节点的子树还要保持有all各节点,那么可以断掉新加的子节点。
            for(int here=1;here<all;here++)
            {
                dp[x][all]= min(dp[x][all],dp[x][here]+dp[y][all-here]);//然而保持all还有其他方式

            }

        }
    }



}
int main()
{
    int x,y;
    while(~scanf("%d%d",&n,&p))
    {

        for(int i=1;i<=n;i++) son[i].clear();

        memset(vis,0,sizeof vis);
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&x,&y);
            son[x].push_back(y);
            vis[y]=1;
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])  {root=i;break;}
        }
        dfs(root,-1);
        int ans=dp[root][p];
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,dp[i][p]+1 );
        }
        printf("%d\n",ans);


    }




    return 0;
}





                                  dp[i][j]表示达到根节点为i,节点数为j的状态,需要的操作次数
状态转移方程:每新考虑一个节点y,dp[x][all]= min(dp[x][all](原来的)+1,dp[x][here]+dp[y][all-here]);  




#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
const int maxn= 150+5   ;
//const int maxm=    ;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
// by yskysker123
int n,p,root;
vector<int> son[maxn];
bool vis[maxn];
int dp[maxn][maxn];//dp[i][j]表示达到根节点为i,节点数为j的状态,需要的操作次数

void DP(int x)
{


    for(int i=0;i<son[x].size();i++)//首先考虑没有子节点的情况,然后子节点一个一个考虑进去
    {
        int y=son[x][i];

        if(vis[y])  continue;
        vis[y]=1;
        DP(y);
        for(int all=p;all>=1;all--)   
        {
              dp[x][all]+=1;           //新加一个节点,如果x为根节点的子树还要保持有all各节点,那么可以断掉新加的子节点。
            for(int here=1;here<=all-1;here++)
            {
                dp[x][all]= min(dp[x][all],dp[x][here]+dp[y][all-here]);//然而保持all还有其他方式

            }

        }
    }

}
int main()
{
    int x,y;
    while(~scanf("%d%d",&n,&p))
    {

        for(int i=1;i<=n;i++)
        {
            son[i].clear();
            for(int j=1;j<=p;j++)//
                dp[i][j]=INF;  
            dp[i][1]=0;

        }

        memset(vis,0,sizeof vis);
        for(int i=1;i<=n-1;i++)
        {
            scanf("%d%d",&x,&y);
            son[x].push_back(y);
            vis[y]=1;
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])  {root=i;break;}
        }
        memset(vis,0,sizeof vis);
        vis[root]=1;
        DP(root);
        int ans=dp[root][p];
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,dp[i][p]+1 );
        }
        printf("%d\n",ans);


    }




    return 0;
}



16-7-31:
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=150    ;
int n,p;
int fir[maxn+10],nex[2*maxn+10],to[2*maxn+10],nedge;
int dp[maxn+10][maxn+10],ans;

bool vis[maxn+10];
int root;

/*
坑点:
1.首先要判断根
2.只要分割出的某棵子树有p个点即可,不一定包含根结点


*/
void add_edge(int x,int y)
{
    to[nedge]=y;
    nex[nedge]=fir[x];
    fir[x]=nedge++;
}
void update(int &x,int val)
{
    if(x<0||val<x)  x=val;
}

int dfs(int x)
{
    dp[x][0]=0;
    int num=1;
    for(int i=fir[x];~i;i=nex[i])
    {
        int y=to[i];
        num+=dfs(y);
        for(int v=num;v>=0;v--)
        {
            int ret=dp[x][v];
            for(int v2=0;v2<=v;v2++) if(~dp[x][v-v2]&&~dp[y][v2])
            {
                update(ret,dp[x][v-v2]+dp[y][v2]);
            }
            dp[x][v]=ret;
        }
    }
    dp[x][num]=1;
    if(x!=root&& n-num<=p )  ans=min(ans,dp[x][p-(n-num)   ]+ 1);
    return num;
}


int main()
{
   std::ios::sync_with_stdio(false);
   while(cin>>n>>p)
   {
       nedge=0;
       mes(fir,-1,n+1);
       mes(vis,0,n+1);
       p=n-p;
       int x,y;
       for1(i,n-1)
       {
           cin>>x>>y;
           vis[y]=1;
           add_edge(x,y);
       }
       mes(dp,-1,n+1);
       for1(i,n)  if(!vis[i])
       {
           root=i;break;
       }
       ans=INF;
       dfs(root);
       cout<<min(dp[root][p],ans  )<<endl;
   }
   return 0;
}

/*
3 1
1 2
1 3


3 2
1 2
1 3

3 3
1 2
1 3


10 6
1 2
2 3
2 4
5 6
5 8
6 7
8 9
8 10
1 5
ans=2

10 4
1 2
2 3
2 4
5 6
5 8
6 7
8 9
8 10
1 5
ans=1



*/



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值