-
Notifications
You must be signed in to change notification settings - Fork 2
/
Maximum Bipartite Matching
78 lines (51 loc) · 1.06 KB
/
Maximum Bipartite Matching
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
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
vector<int>match;
vector<int>vis;
bool solve(int p,vector<vector<int>>&g){
for(int j=0;j<g[0].size();j++){
if(g[p][j] && !vis[j]){
vis[j]=1;
if(match[j]==-1 || solve(match[j],g)){
match[j]=p;
return true;
}
}
}
return false;
}
int maximumMatch(vector<vector<int>>&g){
int p=g.size();
int j=g[0].size();
match=vector<int>(j,-1);
int ans=0;
for(int i=0;i<p;i++){
vis=vector<int>(j,0);
if(solve(i,g))ans++;
}
return ans;
}
};
//{ Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int m, n;
cin >> m >> n;
vector<vector<int>>G(m, vector<int>(n, 0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++)
cin >> G[i][j];
}
Solution obj;
int ans = obj.maximumMatch(G);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends