HDU 1054 Strategic Game

ProblemDescription

Bob enjoys playingcomputer games, especially strategic games, but sometimes he cannot find thesolution 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 theminimum 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 fora given tree.

The input file contains several data sets in text format. Each data setrepresents 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_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 <n <= 1500). Every edge appears only once in the input data.

For example for the tree:
 

                             
 

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

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

 

 

Sample Input

4

0:(1) 1

1:(2) 2 3

2:(0)

3:(0)

5

3:(3) 1 4 2

1:(1) 0

2:(0)

0:(0)

4:(0)

 

 

Sample Output

1

2

 

ProblemDescription

Bob enjoys playingcomputer games, especially strategic games, but sometimes he cannot find thesolution 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 theminimum 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 fora given tree.

The input file contains several data sets in text format. Each data setrepresents 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_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 <n <= 1500). Every edge appears only once in the input data.

For example for the tree:
 

                             
 

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

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

 

 

Sample Input

4

0:(1) 1

1:(2) 2 3

2:(0)

3:(0)

5

3:(3) 1 4 2

1:(1) 0

2:(0)

0:(0)

4:(0)

 

 

Sample Output

1

2

题目大意:有n个点。接着输入每个点的连接关系。图为无向图,但每条边只会描述一次,如:1,4相连,那么1中出现了4,4中就不会出现1了。选取一个点,与他相连的边都被覆盖了。现在要求,将所有边被覆盖需要选取最少点的个数。

dp[i]表示的是以i为根的子树,在i上放置一个士兵,看守住整个子树需要多少士兵。all[ i ]表示看守住整个以i为根的子树需要多少士兵。sum表示的是以i为根的子树,在i上不放置士兵,看守住整个子树需要多少士兵。

因为对于一条边来说,两点至少选取一个点。所以,如果i点不放置士兵,那么他的所有子树上一定都放有士兵,故sum =∑dp[j](ji的子树)。如果i放置士兵,那么对于其子树来说就无所谓了,选取看守子节点为根节点的整个子树的最小值即可,故dp[i] = 1 + ∑all[j](ji的儿子)。那么,对于一棵以i为根的树来说,all[i] = min(dp[i],sum)

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;

int n;
vector<int> v[1510];
int dp[1510] ,all[1510];

void solve(int x ,int pre)
{
    int sum = 0;
    dp[x] = 1;
    all[x] = 0;
    for(int i = 0;i < v[x].size();i++)
    {
        if(v[x][i]!=pre)
        {
            solve(v[x][i],x);
            dp[x] += all[v[x][i]];
            sum += dp[v[x][i]];
        }
    }
    all[x] = min(dp[x],sum);
}

int main()
{
    while(~scanf("%d",&n))
    {
        int index ,num ,no;
        memset(dp,0,sizeof(dp));
        memset(all,0,sizeof(all));
        for(int i = 0;i<n;i++)
        {
            scanf("%d:(%d)",&index,&num);
            for(int j = 0;j<num;j++)
            {
                scanf("%d",&no);
                v[index].push_back(no);
                v[no].push_back(index);
            }
        }
        solve(0,-1);
        printf("%d\n",all[0]);
        for(int i = 0;i<n;i++)
        {
            v[i].clear();
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值