poj 1947 Rebuilding Roads(基础)

来源:互联网 发布:路由器端口号映射 编辑:程序博客网 时间:2024/06/01 22:26

题目大概是给一棵树,问最少删几条边可以出现一个包含点数为p的连通块。

#include <set>#include <map>#include <stack>#include <queue>#include <deque>#include <cmath>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF  0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-9#define maxn 1100010#define MOD 1000000007struct Edge{    int to,next;}edge[220];int n,m;int head[220],tot;int dp[220][220];void add(int u,int v){    edge[tot].to = v;    edge[tot].next = head[u];    head[u] = tot++;}void dfs(int u){    dp[u][1] = 0;    for(int i = head[u]; i != -1; i = edge[i].next)    {        int v = edge[i].to;        dfs(v);        for(int j = m; j >= 1; j--)        {            dp[u][j]++;            for(int k = 1; k < j; k++)                dp[u][j] = min(dp[u][j],dp[u][j-k]+dp[v][k]);        }    }}int main(){    int t,C = 1;    //scanf("%d",&t);    while(scanf("%d%d",&n,&m) != EOF)    {        tot = 0;        memset(head,-1,sizeof(head));        for(int i = 1; i < n; i++)        {            int u,v;            scanf("%d%d",&u,&v);            add(u,v);        }        memset(dp,INF,sizeof(dp));        dfs(1);        int ans = INF;//        for(int i = 1; i <= n; i++)//            for(int j = 1; j <= m;j++ )//            printf("%d %d %d\n",i,j,dp[i][j]);        for(int i = 1; i <= n; i++)            ans = min(ans,dp[i][m]+(i==1?0:1));        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击