天天看点

Strategic Game (二分图最小覆盖数)

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: 

Input

40:(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)      

Output

12

Sample Input

40:(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

12

题目大概:

有很多城市相连,然后要求派很多士兵,需要检测到所有城市,一个士兵只能检测相邻的城市和自己所在城市,问最少派的士兵数。

思路:

很明显的,是一个最小覆盖数的题,每个边都至少要有一个点派士兵,就是需要在集合中,那么按照城市连边建模,求匈牙利就行了。

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 1510
using namespace std;

vector<int>G[MAXN];
int pipei[MAXN];
bool used[MAXN];
int N,T,M;
void init()
{
    for(int i=0;i<N;i++)G[i].clear();
    memset(G,0,sizeof(G));
}
void getMap()
{
    int n;
    int u,v;
    for(int i=0;i<N;i++)
    {
        scanf("%d:(%d)",&u,&n);
        //printf("%d %d\n",u,n);
        for(int j=0;j<n;j++)
        {
            scanf(" %d",&v);
            G[u].push_back(v);
            G[v].push_back(u);
            //G[u][v]=G[v][u]=1;
        }


    }

}
int find(int x)
{
    for(int i = 0; i <G[x].size(); i++)
    {
        int y = G[x][i];
        if(!used[y])
        {
            used[y] = true;
            if(pipei[y] == -1 || find(pipei[y]))
            {
                pipei[y] = x;
                return 1;
            }
        }
    }
    return 0;
}

void solve()
{
    int ans = 0;
    memset(pipei, -1, sizeof(pipei));
    for(int i = 0; i <N; i++)
    {
        memset(used, false, sizeof(used));
        ans += find(i);
    }
    printf("%d\n",ans/2);
}

int main()
{


    while(~scanf("%d",&N))
    {
        init();
        getMap();
        solve();
    }
    return 0;
}