HDU-1054

Strategic Game

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444    Accepted Submission(s): 2520


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
 
Source
 
Recommend
JGShining
/**
          题意:    给出一个图,求最小顶点覆盖 
          做法:二分图最大匹配,最小顶点覆盖 == 最大匹配(双向图)/2;匈牙利算法,时间复杂度是O(VE),
**/
#include<iostream>
#include<cmath>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#define maxn 1570
using namespace std;
int g[maxn][maxn];
int linker[maxn];
int head[maxn];
int used[maxn];
int num = 0;
int n;
int tot = 0;
struct Node
{
          int to;
          int next;
}edge[maxn*10];
void Init()
{
          tot = 0;
          memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
          edge[tot].to = v;
          edge[tot].next = head[u];
          head[u] = tot++;
}
bool dfs(int u)
{
          for(int v = head[u];~v;v = edge[v].next)
          {
                    int tt = edge[v].to;
                    if(used[tt] == 0 )
                    {
                              used[tt] = 1;
                              if(linker[tt] == -1||dfs(linker[tt]))
                              {
                                        linker[tt] = u;
                                        return true;
                              }
                    }
          }
          return false;
}
int hungary()
{
    memset(linker,-1,sizeof(linker));
    int res = 0;
    for(int i=0; i<n; i++)
    {
        memset(used,0,sizeof(used));
        if(dfs(i)) res++;
    }
    return res;
}
int main()
{
//#ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
    while(~scanf("%d",&n))
    {
        int u,v,Q;
        Init();
        memset(g,0,sizeof(g));
        for(int i=0; i<n; i++)
        {
                              scanf("%d:(%d)",&u,&Q);
                              while(Q--)
                              {
                                        scanf("%d",&v);
                                        addedge(u,v);
                                        addedge(v,u);
                              }
        }
        int res = hungary();
        printf("%d\n",res/2);
    }
}

 

posted on 2015-04-04 19:48  `Elaine  阅读(137)  评论(0编辑  收藏  举报

导航