-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBipartite Matching.cpp
92 lines (74 loc) · 1.93 KB
/
Bipartite Matching.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <cstdio>
#include <string.h>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
#define MAXN 300 /* How many vertices in U+V (in total) */
#define N 300
#define EPS 1e-9
#define SQR(x) ((x)*(x))
int r [MAXN][MAXN]; /* MODIFIED Adj. matrix (see note) */
int parent[MAXN];
int cost[MAXN];
int match[MAXN];
int nodes,edges,s,t,m,n;
void addEdge(int x, int y){
// r[x][y+n] = r[y+n][x] = 1;
r[x][y+n] = 1;
}
bool bfs(){
queue<int> q;
for(int i=0;i<nodes;i++)parent[i]=-1;
cost[s] = INT_MAX;
parent[s] = s;
q.push(s);
while (!q.empty()) {
int u =q.front();q.pop();
if (u == t)
return true;
for (int i = 0; i < nodes; i++)
if (r[u][i] > 0 && parent[i]==-1) {
parent[i] = u;
cost[i] = min(cost[u], r[u][i]);
q.push(i);
}
}
return false;
}
int main() {
int st,en;
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while (scanf("%d %d",&n, &m)==2) {
memset(r, 0, sizeof(r)); /* Clear edges */
memset(match,-1,sizeof(match));
scanf("%d", &edges); /* get # of edges */
for(int i=0;i<edges;i++){
scanf("%d %d", &st, &en); /* add edges */
addEdge(st,en); /* Edges [0,n-1]->[0,m-1] */
}
s = m+n;
t = m+n+1;
nodes = n + m + 2;
for(int i=0;i<n;i++)r[s][i]=1;
for(int i=n;i<m+n;i++)r[i][t]=1;
int maxflow = 0;
while (bfs()) {
maxflow += cost[t];
int u = t;
while (parent[u] != u) {
match[parent[u]] = u;
r[parent[u]][u] -= cost[t];
r[u][parent[u]] += cost[t];
u = parent[u];
}
}
printf("%d\n",maxflow);
// for(int i=0;i<n;i++){
// if(match[i]!=-1)printf("%d %d\n",i,match[i]);
// }
}
}