zoukankan      html  css  js  c++  java
  • hdu 1054 Strategic Game (二分匹配)

    Strategic Game

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


    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   |   We have carefully selected several similar problems for you:  1083 1507 1269 1528 1052 
     

     题意:求最少要多少个士兵站岗可使全部点都能观察得到。

             因为是树形结构,每个点都会有一条边,最大匹配数即为题解(不证明了..)。

             和 hdu1068 差不多,一样的模板,不过这题是求最大匹配数,匈牙利后结果除二即为解。

     1 //234MS    280K    1166 B    C++
     2 #include<stdio.h>
     3 #include<string.h>
     4 #define N 1505
     5 struct node{
     6     int v;
     7     int next;
     8 }edge[10*N];
     9 int match[N];
    10 int vis[N];
    11 int head[N];
    12 int n,edgenum;
    13 void addedge(int u,int v)
    14 {
    15     edge[edgenum].v=v;
    16     edge[edgenum].next=head[u];
    17     head[u]=edgenum++;
    18 }
    19 int dfs(int x)
    20 {
    21     for(int i=head[x];i!=-1;i=edge[i].next){
    22         int v=edge[i].v;
    23         if(!vis[v]){
    24             vis[v]=1;
    25             if(match[v]==-1 || dfs(match[v])){
    26                 match[v]=x;
    27                 return 1;
    28             }
    29         }
    30     }
    31     return 0;
    32 }
    33 int hungary()
    34 {
    35     int ret=0;
    36     memset(match,-1,sizeof(match));
    37     for(int i=0;i<n;i++){
    38         memset(vis,0,sizeof(vis));
    39         ret+=dfs(i);
    40     }
    41     return ret;
    42 }
    43 int main(void)
    44 {
    45     int a,b,m;
    46     while(scanf("%d",&n)!=EOF)
    47     {
    48         edgenum=0;
    49         memset(head,-1,sizeof(head));
    50         for(int i=0;i<n;i++){
    51             scanf("%d:(%d)",&a,&m);
    52             while(m--){
    53                 scanf("%d",&b);
    54                 addedge(a,b);
    55                 addedge(b,a);
    56             }
    57         } 
    58         printf("%d
    ",hungary()/2);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    3名百度 ,京东,腾讯被辞退的高级Android工程师现在怎么了?30岁真的是“罪”吗
    Python 开发者在迁移到 Go 前需要知道的事情
    centos7 常用命令--查看当前用户的4种方法
    Centos7找不到ifconfig和netstat命令
    Centos 7 修改日期和时间的命令
    如何利用MobaX同时处理多台虚拟机输入相同命令如何利用MobaX同时处理多台虚拟机输入相同命令
    配置坑了我好久:Logback按天生成文件失效
    quartz系列文章
    SpringBoot使用多实例QUARTZ出现重复执行问题
    IDEA多个springboot项目启动修改端口
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3745920.html
Copyright © 2011-2022 走看看