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

来源:互联网 发布:中银淘宝校园卡注销 编辑:程序博客网 时间:2024/04/30 05:45
点击打开链接
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal 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 very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? 

Your program should find the minimum number of soldiers that Bob has to put for a given tree. 

For example for the tree: 

the solution is one soldier ( at the node 1).

Input

The input contains several data sets in text format. Each data set represents a tree with the following description: 

  • the number of nodes 
  • the description of each node in the following format 
    node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads 
    or 
    node_identifier:(0) 

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

Output

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

Sample Input

40:(1) 11:(2) 2 32:(0)3:(0)53:(3) 1 4 21:(1) 02:(0)0:(0)4:(0)

Sample Output

12

Source

Southeastern Europe 2000

给你一颗树,让你求最小点覆盖。
最小点覆盖:从V中取尽量少的点组成一个集合,使得E中所有边都与取出来的点相连。
dp[i][0]表示点i属于点覆盖,并且以点i为根的子树中所连接的边都被覆盖的情况下点覆盖集中所包含最少点的个数。
dp[i][1]表示点i不属于点覆盖,且以i为根的子树中所连接的边都被覆盖的情况下点覆盖集中所包含最少点的个数。
对于第一种状态dp[i][0],等于每个儿子节点的两种状态的最小值之和+1,方程为:dp[i][0]=1+∑(p[u]=i)min(dp[u][0],dp[u][1]).
对于第二种状态dp[i][1],要求所有与i连接的边都被覆盖,但是i点不属于点覆盖,那么i点所有孩子子节点都必须属于覆盖点,即对于点i的第二种状态与所有子节点的第一种状态有关,在树枝上等于所有子节点的第一种状态的和,方程为:dp[i][1]=∑(p[u]=i)dp[u][0].
//6508K375MS#include<stdio.h>#include<string.h>#include<algorithm>#define M 1507using namespace std;int head[M],nodes;int dp[M][M];struct E{    int v,next;}edge[M*M];void addedge(int u,int v){    edge[nodes].v=v;edge[nodes].next=head[u];    head[u]=nodes++;}void DP(int u,int p){    dp[u][0]=1;    dp[u][1]=0;    int k,v;    for(k=head[u];k!=-1;k=edge[k].next)    {        v=edge[k].v;        if(v==p)continue;        DP(v,u);        dp[u][0]+=min(dp[v][0],dp[v][1]);        dp[u][1]+=dp[v][0];    }}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        memset(head,-1,sizeof(head));        nodes=0;        int u,k,v;        for(int i=0;i<n;i++)        {            scanf("%d:(%d)",&u,&k);            while(k--)            {                scanf("%d",&v);                addedge(u,v);                addedge(v,u);            }        }        DP(0,0);        printf("%d\n",min(dp[0][0],dp[0][1]));    }    return 0;}


0 0