This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
/
MBM.cpp
77 lines (68 loc) · 1.71 KB
/
MBM.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
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
//Problem Statement - Given number of jobs and number of applicants
//And for each applicant given that wether each applicant is
//eligible to get the job or not in the form of matrix
#include<bits/stdc++.h>
using namespace std;
int nApplicants,nJobs; //Total number of applicant and total number of jobs
//Return 1 if a person can get the job
int dfs(vector<vector<int> > graph,int applicant ,int visited[], int result[])
{
// cout<<"dsf"<<endl;
for(int i=0;i<nJobs;i++)
{
if(graph[applicant][i]==1 && !visited[i] )
{
visited[i]=1;
// int checkVal = dfs(graph,result[i],visited,result);
if(result[i]<0 || dfs(graph,result[i],visited,result))
{
result[i]=applicant;
return 1;
}
}
}
return 0;
}
//Return maximum beople that can get the job
int bipartite(vector<vector<int> > graph)
{
int result[nJobs];
for(int i=0;i<nJobs;i++)
{
result[i]=-1;
}
int retval =0;
for(int applicant=0;applicant<nApplicants;applicant++)
{
int visited[nApplicants];
memset(visited,0,sizeof(visited));
if(dfs(graph,applicant,visited,result))
{
retval++;
}
}
return retval;
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cout<<"Enter the number of jobs : ";
cin>>nJobs;
cout<<"Enter the number of applicants : ";
cin>>nApplicants;
vector<vector<int> > graph(nApplicants);
//Taking input if a user can have a job then its value for job is 1
for(int i=0;i<nApplicants;i++)
{
cout<<"Enter the status(1/0) for applicant - "<<i+1<<" for "<<nJobs<<" Jobs!"<<endl;
for(int j=0;j<nJobs;j++)
{
cout<<"For job - "<<j+1<<" : ";
int x;
cin>>x;
graph[i].push_back(x);
}
}
cout<<"Maximum applicants that can have job is : "<<bipartite(graph);
}