二分图的最大匹配板子

二分图的最大匹配

#include <bits/stdc++.h>
using namespace std;

const int N = 510, M = 1e5 + 10;

int n1, n2, m;
int h[N], e[M], ne[M], idx;
bool st[N];
int match[N];

void add(int a, int b){
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
bool find(int u){
    for(int i = h[u]; i != -1; i = ne[i]){
        int j = e[i];
        if(!st[j]){
            st[j] = true;
            if(match[j] == 0 || find(match[j])){match[j] = u; return true;}
        }
    }
    return false;
}
signed main(){
    memset(h, -1, sizeof h);
    scanf("%d%d%d", &n1, &n2, &m);
    while(m--){
        int a, b; scanf("%d%d", &a, &b);
        add(a, b);
    }
    
    int res = 0;
    for(int i = 1; i <= n1; ++i){
        memset(st, 0, sizeof st);
        if(find(i)) ++res;
    }
    
    printf("%d", res);
    
    return 0;
}
posted @ 2025-04-07 20:46  awei040519  阅读(2)  评论(0)    收藏  举报