zoukankan      html  css  js  c++  java
  • 自用二分图匹配模板

    关于二分图匹配的讲解请看我之前的那篇博客。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #define maxn 1005
     5 using namespace std;
     6 struct Edge{
     7     int from,to;
     8 };
     9 inline int read(){
    10     int num = 0;
    11     char c;
    12     bool flag = false;
    13     while ((c = getchar()) == ' ' || c == '
    ' || c == '
    ');
    14     if (c == '-')
    15         flag = true;
    16     else
    17         num = c - '0';
    18     while (isdigit(c = getchar()))
    19         num = num * 10 + c - '0';
    20     return (flag ? -1 : 1) * num;
    21 }
    22 Edge edge[maxn*maxn];
    23 int head[maxn*2];
    24 int tot = 0;
    25 void add_edge(int from,int to){
    26     edge[++tot].from = head[from];
    27     edge[tot].to = to;
    28     head[from] = tot;
    29 }
    30 int n,m,e;
    31 bool vis[maxn];
    32 int match[maxn];
    33 bool dfs_xyl(int x){
    34     for (int i=head[x];i;i=edge[i].from){
    35         int v = edge[i].to;
    36         if (!vis[v]){
    37             vis[v] = true;
    38             if (!match[v]||dfs_xyl(match[v])){
    39                 match[v] = x;
    40                 return true;
    41             }
    42         }
    43     }
    44     return false;
    45 }
    46 int main(){
    47     n = read();m = read();e = read();
    48     for (int i=1;i<=e;i++){
    49         int u,v;
    50         u = read();v = read();
    51         if (u > n || v > m)
    52             continue;
    53         add_edge(u,v);
    54         
    55     }
    56     int ans = 0;
    57     for (int i=1;i<=n;++i){
    58         memset(vis,false,sizeof(vis));
    59         if(dfs_xyl(i))
    60             ans++;
    61     }
    62     cout << ans << endl;
    63     return 0;
    64 }
  • 相关阅读:
    Distinct Substrings(spoj 694)
    Musical Theme
    Milk Patterns(poj 3261)
    Repeated Substrings(UVAlive 6869)
    喵星球上的点名(bzoj 2754)
    滑雪与时间胶囊(bzoj 2753)
    莫比乌斯函数之和(51nod 1244)
    欧拉函数之和(51nod 1239)
    数表(bzoj 3529)
    欧拉函数模板
  • 原文地址:https://www.cnblogs.com/OIerShawnZhou/p/7789964.html
Copyright © 2011-2022 走看看