• 二分图之最小路径覆盖 HDU1151


    在一个有向图中,路径覆盖就是在图中找一些路经,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联
    最小路径数 = 总点数 – 最大匹配数
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <map>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 int head[510];
     9 bool vis[510];
    10 int link[510];
    11 int num;
    12 
    13 struct edge
    14 {
    15     int fro;
    16     int to;
    17     int next;
    18 }e[250000];
    19 
    20 void addEdge(int _fro,int _to)
    21 {
    22     num++;
    23     e[num].fro=_fro;
    24     e[num].to=_to;
    25     e[num].next=head[_fro];
    26     head[_fro]=num;
    27 }
    28 
    29 int find(int x)
    30 {
    31     for(int i=head[x];i!=-1;i=e[i].next)
    32     {
    33         if(!vis[e[i].to])
    34         {
    35             int q=link[e[i].to];
    36             link[e[i].to]=e[i].fro;
    37             vis[e[i].to]=true;
    38             if(q==-1||find(q))
    39                 return 1;
    40             link[e[i].to]=q;
    41         }
    42     }
    43     return 0;
    44 }
    45 
    46 int main()
    47 {
    48     int T;
    49     int noi,nos;
    50     scanf("%d",&T);
    51     while(T--)
    52     {
    53         int ans=0;
    54         num=0;
    55         int a,b;
    56         scanf("%d%d",&noi,&nos);
    57         memset(link,-1,sizeof(link));
    58         memset(head,-1,sizeof(head));
    59         for(int i=1;i<=nos;i++)
    60         {
    61             scanf("%d%d",&a,&b);
    62             addEdge(a,b);
    63         }
    64         for(int i=1;i<=noi;i++)
    65         {
    66             memset(vis,false,sizeof(vis));
    67             ans+=find(i);
    68         }
    69         cout<<noi-ans<<endl;
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    Codeforces Round #172 (Div. 2) B. Nearest Fraction
    什么是DWR
    1310 N皇后问题
    ural Bus Routes(dfs深搜)
    ural Russian Pipelines(最短路)
    ural Graph Decomposition
    ural Network ( 最小生成树)
    poj 1579 Function Run Fun ( 记忆化搜索 )
    计算某一天的前一天的日期
    DataStructGraphpart1
  • 原文地址:https://www.cnblogs.com/wsruning/p/4766403.html
Copyright © 2020-2023  润新知