zoukankan      html  css  js  c++  java
  • *HDU 1054 二分图

    Strategic Game

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


    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
     
    题意:
    找二分图最小点覆盖
    代码:
     1 // 模板  最小点覆盖=最大匹配(有向图);最小点覆盖=最大匹配/2(无向图); 本题数据较大要用邻接表优化。(假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边。)
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<vector>
     6 using namespace std;
     7 int vis[1502],link[1502];
     8 int Mu,Mv,n;  //Mu,Mv分别是左集合点数和右集合点数
     9 vector<int>mp[1502];
    10 int dfs(int x)  //找增广路
    11 {
    12     for(int i=0;i<mp[x].size();i++)
    13     {
    14         int y=mp[x][i];
    15         if(!vis[y])  
    16         {
    17             vis[y]=1;
    18             if(link[y]==-1||dfs(link[y]))
    19             {
    20                 link[y]=x;
    21                 return 1;
    22             }
    23         }
    24     }
    25     return 0;
    26 }
    27 int Maxcon()
    28 {
    29     int ans=0;
    30     memset(link,-1,sizeof(link));
    31     for(int i=0;i<Mu;i++)
    32     {
    33         memset(vis,0,sizeof(vis));
    34         if(dfs(i)) ans++;
    35     }
    36     return ans;
    37 }
    38 int main()
    39 {
    40     int a,b,c;
    41     while(scanf("%d",&n)!=EOF)
    42     {
    43         for(int i=0;i<=n;i++)
    44         mp[i].clear();
    45         memset(mp,0,sizeof(mp));
    46         for(int i=0;i<n;i++)
    47         {
    48             scanf("%d:(%d)",&a,&c);
    49             while(c--)
    50             {
    51                 scanf("%d",&b);
    52                 mp[a].push_back(b);
    53                 mp[b].push_back(a);
    54             }
    55         }
    56         Mv=Mu=n;
    57         printf("%d
    ",Maxcon()/2);
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    面向消息的持久通信与面向流的通信
    通信协议
    分布式系统简介
    Hadoop on Yarn 各组件详细原理
    Parquet文件结构笔记
    Redis部分数据结构方法小结
    Storm Ack框架笔记
    MapReduce格式与类型
    Hadoop 2.6 MapReduce运行原理详解
    Hadoop SequenceFile数据结构介绍及读写
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6066331.html
Copyright © 2011-2022 走看看