-
Notifications
You must be signed in to change notification settings - Fork 439
/
Bigraph-Matching(Improved-Shortest-Augmenting-Path).cpp
121 lines (110 loc) · 1.82 KB
/
Bigraph-Matching(Improved-Shortest-Augmenting-Path).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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <cstdio>
#define INF 1000000000
using namespace std;
struct vertex
{
int first, dis;
}V[1010];
struct edge
{
int endp, next, flow;
}E[502010];
int nl, nr, iec, ec = 2, src = 1005, sink = 1006, gap[1010];
inline int min(int x, int y)
{
return x < y ? x : y;
}
void init()
{
gap[0] = nl + nr + 2;
}
void add_edge(int u, int v, int f)
{
E[ec].next = V[u].first;
V[u].first = ec;
E[ec].endp = v;
E[ec].flow = f;
ec++;
}
int isap(int u, int curf)
{
if (u == sink)
{
return curf;
}
int totalf = 0, mindis = nl + nr + 2;
for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next)
{
if (E[cur].flow > 0)
{
if (V[u].dis == V[E[cur].endp].dis + 1)
{
int f = isap(E[cur].endp, min(curf - totalf, E[cur].flow));
E[cur].flow -= f;
E[cur ^ 1].flow += f;
totalf += f;
}
if (V[E[cur].endp].dis < mindis)
{
mindis = V[E[cur].endp].dis;
}
}
}
if (totalf == 0)
{
if (--gap[V[u].dis] == 0) V[src].dis = nl + nr + 2;
V[u].dis = mindis + 1;
gap[V[u].dis]++;
}
return totalf;
}
int max_flow()
{
int res = 0;
while (V[src].dis < nl + nr + 2)
{
res += isap(src, INF);
}
return res;
}
int main()
{
int u, v;
scanf("%d%d%d", &nl, &nr, &iec);
for (int i = 0; i < iec; i++)
{
scanf("%d%d", &u, &v);
add_edge(u, v + nl, 1);
add_edge(v + nl, u, 0);
}
for (int i = 1; i <= nl; i++)
{
add_edge(src, i, 1);
add_edge(i, src, 0);
}
for (int i = 1; i <= nr; i++)
{
add_edge(i + nl, sink, 1);
add_edge(sink, i + nl, 0);
}
init();
printf("%d\n", max_flow());
for (int i = 1; i <= nl; i++)
{
bool has = false;
for (int cur = V[i].first; cur != 0; cur = E[cur].next)
{
if (E[cur].endp != src && E[cur].flow == 0)
{
printf("%d ", E[cur].endp - nl);
has = true;
break;
}
}
if (has == false)
{
printf("0 ");
}
}
return 0;
}