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

来源:互联网 发布:开淘宝商城的条件 编辑:程序博客网 时间:2024/04/30 07:30
Strategic game
Time Limit: 2000MS Memory Limit: 10000KTotal Submissions: 7486 Accepted: 3481

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


题目链接:http://poj.org/problem?id=1463


题目大意:求树的最小点覆盖

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int const MAX = 1505;int head[MAX], cnt;int dp[MAX][2];struct EDGE{    int to, next;}e[MAX * MAX / 2];void Add(int u, int v){    e[cnt].to = v;    e[cnt].next = head[u];    head[u] = cnt ++;}void DFS(int u, int fa){    dp[u][1] = 1;    dp[u][0] = 0;    for(int i = head[u]; i != -1; i = e[i].next)    {        int v = e[i].to;        if(v != fa)        {            DFS(v, u);            dp[u][1] += min(dp[v][0], dp[v][1]);            dp[u][0] += dp[v][1];        }    }}int main(){    int n;    while(scanf("%d", &n) != EOF)    {        cnt = 0;        memset(head, -1, sizeof(head));        int u, num, v, rt;        for(int i = 0; i < n; i++)        {            scanf("%d:(%d)", &u, &num);            if(i == 0)                rt = u;            for(int j = 0; j < num; j++)            {                scanf("%d", &v);                Add(u, v);                Add(v, u);            }        }        if(n == 1)        {            printf("1\n");            continue;        }        memset(dp, 0, sizeof(dp));        DFS(rt, -1);        printf("%d\n", min(dp[rt][0], dp[rt][1]));    }}


0 0
原创粉丝点击