hdu1054--Strategic Game

Strategic Game

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


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: 

hdu1054--Strategic Game_第1张图片 

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
Southeastern Europe 2000 
二分匹配的题目,求解最小覆盖点,最小覆盖点 = 最大匹配数
左右图分别有为n个点,有由输入中如果a 中有b  意味着,左a到右b有一条边,同样左b到右a也有一条边,这样建图,会使最大匹配数变为原来的2倍
n最大有1500点,要使用临界链表存储,不能使用矩阵,会有超时的可能
#include <stdio.h>
#include <string.h>
struct node{
    int v ;
    node *next ;
} *head[1600] ;
int n1 , n2 , m , n , ans ;
int link[1600] , temp[1600] ;
int f(int x)
{
    for( node *q = head[x] ; q != NULL ; q = q->next )
    {
        int k = q->v ;
        if( !temp[k] )
        {
            temp[k] = true ;
            if( link[k] == -1 || f( link[k] ) )
            {
                link[k] = x ;
                return true ;
            }
        }
    }
    return false ;
}
int main()
{
    int i , j , t ;
    while(scanf("%d", &n)!=EOF)
    {
        memset(link,-1,sizeof(link));
        memset(head,NULL,sizeof(head));
        ans = 0 ;
        for(t = 0 ; t < n ; t++)
        {
            scanf("%d: (%d)", &i, &m);
            while(m--)
            {
                scanf("%d", &j);
                node *q = new node ;
                q->v = i ;
                q->next = head[j] ;
                head[j] = q ;
                q = new node ;
                q->v = j ;
                q->next = head[i] ;
                head[i] = q ;
            }
        }
        for(i = 0 ; i < n ; i++)
        {
            memset(temp,0,sizeof(temp));
            if(  f(i) )
            {
                ans++ ;
            }
        }
        printf("%d\n", ans/2 );
    }
    return 0;
}

你可能感兴趣的:(hdu1054--Strategic Game)