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: 

 

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值