hdu1054Strategic Game

原创 2017年01月03日 16:52:51

Strategic Game
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7770 Accepted Submission(s): 3709

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 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.

The input file 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_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 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 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
守卫一座城市,每个节点连接一些边,一个士兵可以守卫一条街,问最少放多少士兵能保证城市安全
就是变相的最小定点覆盖=最大匹配()/2
用邻接表建图,分出两个初始图,然后从1图u点开始像图2任意未匹配节点发出一条边连接,然后寻找一条最短的增广路。
邻接表用STL容器建立。

#include <cstdio>
#include <vector>//用STL容器来建立邻接表
#include <cstring>
#include <algorithm>
#define N 1510
using namespace std;
int link[N],vist[N];
int VN;
vector<int>mp[N];
bool dfs(int u)
{
    for(int i=0; i<mp[u].size(); i++)
    {
        if(!vist[mp[u][i]])
        {
            vist[mp[u][i]]=1;
            if(link[mp[u][i]]==-1||dfs(link[mp[u][i]]))
            {
                link[mp[u][i]]=u;
                return true;
            }
        }
    }
    return false;
}
int hungary()
{
    int cnt=0;
    memset(link,-1,sizeof(link));
    for(int i=0; i<VN; i++)
    {
        memset(vist,0,sizeof(vist));
        if(dfs(i))
            cnt++;
    }
    return cnt;
}
int main()
{
    int u,k,v,n;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<N; i++)
            mp[i].clear();
        for(int i=0;i<n;i++)//while在这不能使用
        {
            scanf("%d:(%d)",&u,&k);
            while(k--)
            {
                scanf("%d",&v);
                mp[u].push_back(v);
                mp[v].push_back(u);
            }
        }
        VN=n;
        printf("%d\n",hungary()/2);
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许可随意转载。

相关文章推荐

HDU1054(最小顶点覆盖)解题报告

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...

HDU1054Strategic Game(最小顶点覆盖数)

我们来先了解一下什么是最小顶点覆盖; 图G的顶点覆盖是一个顶点集合V,使得G中的每一条边都接触V中的至少一个顶点。我们称集合V覆盖了G的边。最小顶点覆盖是用最少的顶点来覆盖所有的边。顶点覆盖数是最...

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

题目链接:点击打开链接 题意描述:给一棵树,找最少的点集能把树上的所有边覆盖掉? 解题思路:典型的最小覆盖点 树形dp即可 dp[i][0]:表示如果不选i点则dp[i][0]+=dp...

hdu_1054最小点覆盖树上

Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...

HDU 1054 最小定点覆盖

点击打开链接 题意:一个顶点要一个人放哨,则和他相连的几个定点就不需要人放哨了,问最少需要多少人放哨 思路:很明显的题意,很明显的最小顶点覆盖,最小顶点覆盖=最大匹配,建个双向边,结果除2,我的二...
  • Dan__ge
  • Dan__ge
  • 2016年04月01日 13:39
  • 432

hdu_1054 树结构dp 最小覆盖数

题目的意思是给你一个树求一个最小的覆盖,有一种做法是把树结构转换为二部图,树结构是一定能转换为二部图结构的,因为树没有 “圈”结构(起点和终点相,但是中间的点都不同) ,所以可以把树的节点染色,一个节...
  • zz_1215
  • zz_1215
  • 2011年12月11日 21:09
  • 453

hdu 1054 Strategic Game 二分图的匹配,最小点覆盖数

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu...
  • winycg
  • winycg
  • 2016年03月21日 17:09
  • 147

HDU 1054 Strategic Game 最小顶点覆盖

问题描述: Problem Description Bob enjoys playing computer games, especially strategic games, but somet...

HDU 1054 Strategic Game(二分图 匈牙利算法)

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu...

hdu1054Strategic Game(树形DP)

Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu1054Strategic Game
举报原因:
原因补充:

(最多只允许输入30个字)