MinimumVertexCover
Finds a minimum vertex cover in a bipartite graph. The size is the same as the size of a maximum matching, and the complement is a maximum independent set.
20 lines stress-tested
Needs: "DFSMatching.h"
content/graph/MinimumVertexCover.h — Johan Sannemo, Simon Lindholm
vi cover(vector<vi>& g, int n, int m) {
vi match(m, -1);
int res = dfsMatching(g, match);
vector<bool> lfound(n, true), seen(m);
for (int it : match) if (it != -1) lfound[it] = false;
vi q, cover;
rep(i,0,n) if (lfound[i]) q.push_back(i);
while (!q.empty()) {
int i = q.back(); q.pop_back();
lfound[i] = 1;
for (int e : g[i]) if (!seen[e] && match[e] != -1) {
seen[e] = true;
q.push_back(match[e]);
}
}
rep(i,0,n) if (!lfound[i]) cover.push_back(i);
rep(i,0,m) if (seen[i]) cover.push_back(n+i);
assert(sz(cover) == res);
return cover;
}