-
Notifications
You must be signed in to change notification settings - Fork 438
/
Hungarian-Algorithm.cpp
73 lines (64 loc) · 1.09 KB
/
Hungarian-Algorithm.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
#include <cstdio>
#include <cstring>
#define VERTEX_COUNT 1010
#define EDGE_COUNT 250010
using namespace std;
struct vertex
{
int first, mv;
}V[VERTEX_COUNT];
struct edge
{
int endp, next;
}E[EDGE_COUNT];
int nl, nr, iec, ec = 2;
bool vis[VERTEX_COUNT];
inline void add_edge(int u, int v)
{
E[ec].next = V[u].first;
V[u].first = ec;
E[ec].endp = v;
ec++;
}
bool dfs(int u)
{
if (vis[u] == true) return false;
vis[u] = true;
for (int cur = V[u].first; cur != 0; cur = E[cur].next)
{
if (V[u].mv != E[cur].endp && (V[E[cur].endp].mv == 0 || dfs(V[E[cur].endp].mv) == true))
{
V[u].mv = E[cur].endp;
V[E[cur].endp].mv = u;
return true;
}
}
return false;
}
int match()
{
int res = 0;
for (int i = 1; i <= nl; i++)
{
memset(vis, false, sizeof(vis));
res += (int)dfs(i);
}
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);
}
printf("%d\n", match());
for (int i = 1; i <= nl; i++)
{
if (V[i].mv != 0) printf("%d ", V[i].mv - nl);
else printf("0 ");
}
return 0;
}