-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3386 【模板】二分图匹配.cpp
64 lines (56 loc) · 1.17 KB
/
P3386 【模板】二分图匹配.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
const int maxm = 1000000 + 10;
struct Edge {
int to, next;
Edge(){}
Edge(int _to, int _next) {
to = _to, next = _next;
}
};
int head[maxn], idx;
Edge edges[maxm];
void add(int u, int v) {
edges[++idx] = Edge(v, head[u]);
head[u] = idx;
}
void se(int u) {
int i;
for(i=head[u]; i!=0; i = edges[i].next) {
cout<<edges[i].to<<endl;
}
}
bool vis[maxn];int lpair[maxn];
bool find(int u) {
register int i, now;
for(i=head[u]; i; i=edges[i].next) {
now = edges[i].to;
if(!vis[now]) {
vis[now] = true;
if(lpair[now]==0 || find(lpair[now])) {
lpair[now] = u;
return true;
}
}
}
return false;
}
int n, m, e;
int main() {
scanf("%d%d%d", &n, &m, &e);
register int i, u, v;
for(i=0; i<e; i++) {
scanf("%d%d", &u, &v);
if(u>n || v>m) continue;
add(u, v);
}
se(1);
register unsigned int cnt = 0;
for(i=1; i<=n; i++) {
memset(vis, 0, sizeof vis);
if(find(i)) cnt++;
}
printf("%d\n", cnt);
return 0;
}