-
Notifications
You must be signed in to change notification settings - Fork 1
/
9th_feb.cpp
35 lines (35 loc) · 1.08 KB
/
9th_feb.cpp
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
class Solution {
public:
vector<int> vis;
vector<int> matching;
bool solve(int person, vector<vector<int>>&G){
for(int job=0;job<G[0].size();job++){
//check if the person is intrested in job
// and the job is not occupied by anyone
// visit that
if(G[person][job]==1 and !vis[job]){
vis[job]=1;
// if the matching is vacant means no one is assigned to it
// or the person who is assigned can be replace to some other job
if(matching[job]==-1 or solve(matching[job],G)){
matching[job]=person;
return true;
}
}
}
return false;
}
int maximumMatch(vector<vector<int>>&G){
int n = G[0].size(), m=G.size();
int ans=0;
matching=vector<int>(n,-1);
// iterating over the persons
for(int i=0;i<m;i++){
//vis jobs
vis=vector<int>(n,0);
if(solve(i,G))
ans++;
}
return ans;
}
};