Skip to content

Commit

Permalink
Create bipartite_matching.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Jun 29, 2024
1 parent 2251e13 commit feec8ed
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ps/Graph/matching/bipartite_matching.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
using ll = long long;
using pll = pair<ll, ll>;
using ld = long double;
int n, m;
int a[1001];
int b[1001];
int visited[1001];
vector<int> edge[1001];
bool dfs(int pos){
visited[pos] = true;
for (int nxt: edge[pos]){
if (b[nxt] == -1 || (!visited[b[nxt]] && dfs(b[nxt]))){
a[pos] = nxt; b[nxt] = pos; return true;
}
}
return false;
}
void bipartite_matching() {
cin >> n >> m;
for (int i = 1; i <= n; i++){
int x; cin >> x;
while (x--){
ll nxt; cin >> nxt;
edge[i].emplace_back(nxt);
}
}
int match = 0;
memset(a, -1, sizeof(a));
memset(b, -1, sizeof(b));
for (int i = 1; i <= n; i++){
if (a[i] == -1){
memset(visited, 0, sizeof(visited));
if (dfs(i)) match++;
}
}
return match;
}

0 comments on commit feec8ed

Please sign in to comment.