hdu1054 Strategic Game(最小覆盖点-树形dp)

题目链接:点击打开链接

题意描述:给一棵树,找最少的点集能把树上的所有边覆盖掉?

解题思路:典型的最小覆盖点

树形dp即可

dp[i][0]:表示如果不选i点则dp[i][0]+=dp[t][1];///表示要选所有的儿子节点

dp[i][1]:表示如果选i点则dp[i][1]+=min(dp[t][0],dp[t][1]);///表示选择其儿子节点中较小的情况

代码:

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAXN 1510
using namespace std;
int head[MAXN],tol;
struct Edge{
    int v,next;
}edge[2*MAXN];
void addEdge(int u,int v){
    edge[tol].v=v;edge[tol].next=head[u];head[u]=tol++;
    edge[tol].v=u;edge[tol].next=head[v];head[v]=tol++;
}
int dp[MAXN][2];
void DP(int u,int p){
    dp[u][0]=0;
    dp[u][1]=1;
    int k,to;
    for(k=head[u];k!=-1;k=edge[k].next){
        to=edge[k].v;
        if(to==p) continue;
        DP(to,u);
        dp[u][0]+=dp[to][1];
        dp[u][1]+=min(dp[to][0],dp[to][1]);
    }
}
int n;
int main(){
    while(scanf("%d",&n)!=EOF){
        int u,v,num;
        tol=0;memset(head,-1,sizeof(head));
        for(int i=0;i<n;++i){ scanf("%d:(%d)",&u,&num);
            for(int j=0;j<num;++j){ scanf("%d",&v);addEdge(u,v);}
        }
        DP(0,0);
        printf("%d\n",min(dp[0][0],dp[1][1]));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 01:31:01

hdu1054 Strategic Game(最小覆盖点-树形dp)的相关文章

Strategic game(POJ 1463 树形DP)

Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 7490   Accepted: 3483 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad

hdu---(1054)Strategic Game(最小覆盖边)

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5034    Accepted Submission(s): 2297 Problem Description Bob enjoys playing computer games, especially strategic games, but somet

hdu1054 树形dp

http://acm.hdu.edu.cn/showproblem.php?pid=1054 Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must

HDU 1054 Strategic Game(树形DP)

Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which for

UVa 2038 - Strategic game(二分图最小顶点覆盖 or 树形DP)

Strategic game Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of wh

POJ 1463 Strategic game 最小点覆盖集(树形dp)

点击打开链接 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6105   Accepted: 2808 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is v

树形dp compare E - Cell Phone Network POJ - 3659 B - Strategic game POJ - 1463

B - Strategic game POJ - 1463 题目大意:给你一棵树,让你放最少的东西来覆盖所有的边 这个题目之前写过,就是一个简单的树形dp的板题,因为这个每一个节点都需要挺好处理的. 这个树形dp是从底往根部来递推,所以每一个点,都是由它的根节点来递推的. 如果一个根节点的子节点放了东西,那么这个根节点就可以有选择,但是如果没有放东西,那么这个根节点就必须放东西. E - Cell Phone Network POJ - 3659 题目大意:给你一棵树,让你用最小的东西来覆盖所有

Strategic Game(树形DP)

目录 Strategic Game(树形DP) 题目 题意 思路 题解 Strategic Game(树形DP) 题目 Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend

HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,并且该节点不放,所需的最少的点数. dp[i][1]表示i为根节点,并且该节点放,所需要的最少的点数. dp[i][0]=sum(dp[son[i][j]][1]) 该点不放,则它的儿子节点必须都放,只有这样之间的边才可以被覆盖. dp[i][1]=sum(min(dp[son[i][j]][0],dp[son[i][j]][1])) 该点放的话,则它的