二分图最大匹配

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define re register
 6 #define rep(i, a, b) for (re int i = a; i <= b; ++i)
 7 #define repd(i, a, b) for (re int i = a; i >= b; --i)
 8 #define maxx(a, b) a = max(a, b);
 9 #define minn(a, b) a = min(a, b);
10 #define LL long long
11 #define inf (1 << 30)
12 
13 inline int read() {
14     int w = 0, f = 1; char c = getchar();
15     while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar();
16     while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar();
17     return w * f;
18 }
19 
20 const int maxn = 1000 + 5;
21 
22 int e[maxn << 1][maxn << 1], v[maxn], lk[maxn];
23 int n, m, E;
24 
25 bool find(int u) {
26     rep(i, 1, m)
27         if (!v[i] && e[u][i+n]) {
28             v[i] = 1;
29             if (!lk[i] || find(lk[i])) {
30                 lk[i] = u;
31                 return true;
32             }
33         }
34     return false;
35 }
36 
37 int main() {
38     n = read(), m = read(), E = read();
39 
40     rep(i, 1, E) {
41         register int u = read(), v = read();
42         e[u][v+n] = e[v+n][u] = 1;
43     }
44 
45     int cnt = 0;
46     rep(i, 1, n) {
47         memset(v, 0, sizeof(v)); // 如果题目要求比较高,不要用这句话,改为时间戳判断。
48         if (find(i)) cnt++;
49     }
50 
51     printf("%d", cnt);
52     return 0;
53 }

 

posted @ 2019-01-30 21:17  AC-Evil  阅读(172)  评论(0编辑  收藏  举报