-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bipartite Matching (for constant risks)
- Loading branch information
1 parent
8a4e2af
commit 5e96abe
Showing
2 changed files
with
40 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters