Pagini recente » Cod sursa (job #433084) | Cod sursa (job #1656200) | Cod sursa (job #1393397) | Cod sursa (job #684779) | Cod sursa (job #2813063)
#include <bits/stdc++.h>
#define N 3005
#define M 1
#define inf 20000
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m;
bool findMatchBipartite(bool bip_graph[N][N], int vertex, bool seen[], int match[])//if we find a job for the current vertex
{
//a dfs based function that tries all possibilites to assign a job to the applicant
//if a job isn't assigned->we assign it
//if it is assigned yo x, we try to check recursively if x can have another job
//to make sure that we don't assign multiple times the same job to a cnadidate--> seen
for(int i = 1; i <= m; ++ i)//try every job
{
if(bip_graph[i][vertex] && !seen[i])
{
seen[i] =1;//to not repeat it
//if it s not assigned or it is assigned to smb that can have another job
if(match[i] == -1 || findMatchBipartite(bip_graph, match[i], seen, match))
{
match[i] = vertex;
return true;
}
}
}
return false;
}
void maxBipartite()
{
bool bip_graph[N][N];
//lines with jobs and columns with candidates
int e;
fin >> e;
for(int i = 1; i <= e; ++i)
{
int x, y;
fin >> x >> y;
bip_graph[y][x] = 1;
}
//n -- aplicants(left)
//m -- jobs(right)
int match[N];//the applicant for job i, match or -1
memset(match, -1, sizeof(match));
int sol = 0;
for(int i = 1; i <= n; ++i)//for every aplicant
{
bool seen[N];
memset(seen, 0, sizeof(seen));//not seen
if(findMatchBipartite(bip_graph, i,seen, match))
sol++;//found job;
}
fout << sol <<"\n";
for(int i = 1; i <= m; ++i)//for every job that has an applicant
if(match[i] != -1)
fout << match[i] << " " << i << "\n";
}
int main()
{
fin >> n >> m;
maxBipartite();
return 0;
}