17.二分图的最大匹配

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 510, M = 100010;
 4 int h[N], e[M], ne[M], idx;
 5 int n1, n2, m;
 6 bool st[N]; //判重
 7 int match[N]; //右边点对应的点
 8 void add(int a, int b) {
 9     e[idx] = b;
10     ne[idx] = h[a];
11     h[a] = idx++;
12 }
13 bool find(int x) {
14     for (int i = h[x]; i != -1; i = ne[i]) {
15         int j = e[i];
16         if (!st[j]) {
17             st[j] = true;
18             if (match[j] == 0 || find(match[j])) {
19                 match[j] = x;
20                 return true;
21             }
22         }
23     }
24     return false;
25 }
26 int main() {
27     memset(h, -1, sizeof h);
28     cin >> n1 >> n2 >> m;
29     while (m--) {
30         int a, b;
31         cin >> a >> b;
32         add(a, b);
33     }
34     int res = 0;
35     for (int i = 1; i <= n1; i++) { //看每个男生
36         memset(st, false, sizeof st); //清空每个妹子
37         if (find(i)) {
38             res++;
39         }
40     }
41     cout << res << endl;
42     return 0;
43 }
原文地址:https://www.cnblogs.com/fx1998/p/13338389.html