Pagini recente » Cod sursa (job #714079) | Cod sursa (job #96668) | Cod sursa (job #1573281) | Cod sursa (job #2586687) | Cod sursa (job #1556151)
//Complexitate O(E*sqrt(V))
#include <iostream>
#include <stdio.h>
#include <vector>
#include <cstring>
#define NMax 10005
using namespace std;
vector<int> Graf[NMax];
int N,M,E,x,y,CuplajMaxim;
int M1[NMax],M2[NMax];
bool mark[NMax],cuplaj;
void Read()
{
scanf("%d%d%d",&N,&M,&E);
for(int i=1;i<=E;i++)
{
scanf("%d%d",&x,&y);
Graf[x].push_back(y);
}
}
bool Cuplaj(int x)//Hopcroft-Karp
{
if(mark[x])
return false;
mark[x]=true;
for(vector<int>::iterator it=Graf[x].begin();it!=Graf[x].end();it++)
if(M2[*it]==0)
{
M1[x]=*it,M2[*it]=x;
return true;
}
for(vector<int>::iterator it=Graf[x].begin();it!=Graf[x].end();it++)
if(Cuplaj(M2[*it]))
{
M1[x]=*it,M2[*it]=x;
return true;
}
return false;
}
void Solve()
{
cuplaj=true;
while(cuplaj==true)
{
cuplaj=false;
memset(mark,false,sizeof(mark));
for(int i=1;i<=N;i++)
if(M1[i]==0 && Cuplaj(i)==true)
{
cuplaj=true;
CuplajMaxim++;
}
}
}
inline void Write()
{
printf("%d\n",CuplajMaxim);
for(int i=1;i<=N;i++)
if(M1[i])
printf("%d %d\n",i,M1[i]);
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
Read();
Solve();
Write();
}