-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathMaximum Bipartite Matching and Min Vertex Cover.cpp
104 lines (93 loc) · 1.61 KB
/
Maximum Bipartite Matching and Min Vertex Cover.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
int n, m, p; // n = # of nodes on left, m = # of nodes on right
vi bp[N]; // bipartite graph
int matched[N], revmatch[N];
bool seen[N], visited[2][N];
bool trymatch(int u)
{
FOR(j,0,bp[u].size())
{
int v=bp[u][j];
if(seen[v]) continue;
seen[v]=true;
// v is on right, u on left
if(matched[v]<0 || trymatch(matched[v]))
{
matched[v]=u;
revmatch[u]=v;
return true;
}
}
return false;
}
// 0 based
int maxbpm(int sz)
{
ms(matched,-1);
ms(revmatch,-1); // for min-vertex-cover
int ret=0;
FOR(i,0,sz)
{
ms(seen,false);
if(trymatch(i)) ret++;
}
return ret;
}
void dfsLast(int u, bool side)
{
if(visited[side][u]) return;
visited[side][u]=true;
if(!side)
{
for(int i=0; i<n; i++)
{
if(graph[u][i] && matched[u]!=i)
dfsLast(i,1-side);
}
}
else dfsLast(matched[u],1-side);
}
void findMinVertexCover()
{
FOR(i,0,n)
{
if(revmatch[i]==-1)
{
dfsLast(i,0);
}
}
// Assuming both sides have n nodes
vi mvc, mis; // min vertex cover, max independent set
FOR(i,0,n)
{
if(!visited[0][i] || visited[1][i]) mvc.pb(i);
if(!(!visited[0][i] || visited[1][i])) mis.pb(i);
}
}
// The following probably optimizes for large graphs
bool trymatch(int u)
{
// tag is used so that we don't clear seen each time
if(seen[u]==tag) return false;
seen[u]=tag;
FOR(j,0,bp[u].size())
{
int v=bp[u][j];
// first we only consider any matched[v]==-1 case
if(matched[v]<0)
{
matched[v]=u;
return true;
}
}
FOR(j,0,bp[u].size())
{
int v=bp[u][j];
// Now we go deeper and call trymatch
if(trymatch(matched[v]))
{
matched[v]=u;
return true;
}
}
return false;
}