Skip to content

Commit

Permalink
Add Bipartite Matching (for constant risks)
Browse files Browse the repository at this point in the history
  • Loading branch information
baluteshih committed Feb 11, 2024
1 parent 8a4e2af commit 5e96abe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 52 deletions.
88 changes: 38 additions & 50 deletions codebook/4_Flow_Matching/Bipartite_Matching.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,43 @@
struct Bipartite_Matching { // 0-base
int l, r;
int mp[MAXN], mq[MAXN];
int dis[MAXN], cur[MAXN];
vector<int> G[MAXN];
bool dfs(int u) {
for (int &i = cur[u]; i < SZ(G[u]); ++i) {
int e = G[u][i];
if (!~mq[e] || (dis[mq[e]] == dis[u] + 1 && dfs(mq[e])))
return mp[mq[e] = u] = e, 1;
}
dis[u] = -1;
return 0;
int mp[N], mq[N], dis[N + 1], cur[N], l, r;
vector<int> G[N + 1];
bool dfs(int u) {
for (int &i = cur[u]; i < SZ(G[u]); ++i) {
int e = G[u][i];
if (mq[e] == l || (dis[mq[e]] == dis[u] + 1 && dfs(mq[e])))
return mp[mq[e] = u] = e, 1;
}
bool bfs() {
int rt = 0;
queue<int> q;
fill_n(dis, l, -1);
for (int i = 0; i < l; ++i)
if (!~mp[i])
q.push(i), dis[i] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int e : G[u])
if (!~mq[e])
rt = 1;
else if (!~dis[mq[e]]) {
q.push(mq[e]);
dis[mq[e]] = dis[u] + 1;
}
}
return rt;
return dis[u] = -1, 0;
}
bool bfs() {
queue<int> q;
fill_n(dis, l + 1, -1);
for (int i = 0; i < l; ++i)
if (!~mp[i])
q.push(i), dis[i] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int e : G[u])
if (!~dis[mq[e]])
q.push(mq[e]), dis[mq[e]] = dis[u] + 1;
}
int matching() {
int rt = 0;
fill_n(mp, l, -1);
fill_n(mq, r, -1);
while (bfs()) {
fill_n(cur, l, 0);
for (int i = 0; i < l; ++i)
if (!~mp[i] && dfs(i))
++rt;
}
return rt;
}
void add_edge(int s, int t) {
G[s].pb(t);
}
void init(int _l, int _r) {
l = _l, r = _r;
for (int i = 0; i < l; ++i)
G[i].clear();
return dis[l] != -1;
}
int matching() {
int res = 0;
fill_n(mp, l, -1), fill_n(mq, r, l);
while (bfs()) {
fill_n(cur, l, 0);
for (int i = 0; i < l; ++i)
res += (!~mp[i] && dfs(i));
}
return res; // (i, mp[i] != -1)
}
void add_edge(int s, int t) { G[s].pb(t); }
void init(int _l, int _r) {
l = _l, r = _r;
for (int i = 0; i <= l; ++i)
G[i].clear();
}
};
4 changes: 2 additions & 2 deletions codebook/content.tex
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ \subsection{KDTree}
\section{Flow/Matching}
% \subsection{Dinic}
% \lstinputlisting{4_Flow_Matching/Dinic.cpp}
% \subsection{Bipartite Matching} % test by Lib-Checker Matching on Bipartite Graph
% \lstinputlisting{4_Flow_Matching/Bipartite_Matching.cpp}
\subsection{Bipartite Matching*} % test by Lib-Checker Matching on Bipartite Graph
\lstinputlisting{4_Flow_Matching/Bipartite_Matching.cpp}
\subsection{Kuhn Munkres*} % test by CF 101239 C (World Finals' problem)
\lstinputlisting{4_Flow_Matching/Kuhn_Munkres.cpp}
\subsection{MincostMaxflow*} % test by luogu 3381
Expand Down

0 comments on commit 5e96abe

Please sign in to comment.