hdu 1054 Strategic Game 最少点覆盖 二分图最大匹配

Strategic GameTime Limit:10000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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

所以就是一道二分图最大匹配的问题,这题就是输入看起来有点麻烦,其它倒没什么,注意一下点的数量较多,就不要用邻接矩阵啦。


#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

const int MAXN=5010;
const int MAXM=50010;

struct Edge
{
    int to,next;
} edge[MAXM];

int head[MAXN],tol;

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v)
{
    edge[tol].to=v;
    edge[tol].next=head[u];
    head[u]=tol++;
}

int linker[MAXN];
bool used[MAXM];
int uN;

bool dfs(int u)
{
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].to;
        if(!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(int u=0; u<uN; u++)
    {
        memset(used,false,sizeof(used));
        if(dfs(u))
            res++;
    }
    return res;
}

void getdata(char *data,int &node,int &num)
{
    int i;
    node=num=0;
    for(i=0; data[i]!=':'; i++)
    {
        //cout<<data[i]<<endl;
        node*=10;
        node+=data[i]-'0';
    }

    for(i+=2; data[i]!=')'; i++)
    {
        num*=10;
        num+=data[i]-'0';
    }
}

int main()
{
    char data[100];
    int node,num,to,i,j;
    while(~scanf("%d",&uN))
    {
        init();
        for(j=0; j<uN; j++)
        {
            scanf("%s",data);
            getdata(data,node,num);
            //printf("%d %d\n",node,num);
            for(i=0; i<num; i++)
            {
                scanf("%d",&to);
                addedge(node,to);
                addedge(to,node);
            }
        }
        int ans=hungary();
        printf("%d\n",ans/2);


    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值