Pagini recente » Cod sursa (job #696435) | Cod sursa (job #1086600) | Cod sursa (job #1849362) | Cod sursa (job #566505) | Cod sursa (job #1166735)
#include<cstdio>
#include<vector>
#include<bitset>
using namespace std;
const int NMAX = 10000+5;
const int MMAX = 10000+5;
void Read(),Solve(),Print();
int N,M,E,Solution;
int Match_A[NMAX];
int Match_B[MMAX];
vector<int> A[NMAX];
bitset<NMAX> Viz;
int Pair_up(int nod)
{
if(Viz[nod]) return 0;
vector<int>::iterator it;
Viz[nod]=1;
for(it=A[nod].begin(); it!=A[nod].end(); it++)
if(!Match_B[*it] || Pair_up(Match_B[*it]))
{
Match_A[nod]=*it;
Match_B[*it]=nod;
return 1;
}
return 0;
}
int main()
{
Read();
Solve();
Print();
return 0;
}
void Read()
{
int x,y;
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d%d%d",&N,&M,&E);
for(; E; --E)
{
scanf("%d%d",&x,&y);
A[x].push_back(y);
}
}
void Solve()
{
int i,ok;
for(ok=1; ok; )
{
ok=0;
Viz=0;
for(i=1; i<=N; i++)
if(!Match_A[i]) ok|=Pair_up(i);
}
}
void Print()
{
int i;
for(i=1; i<=N; i++)
if(Match_A[i]) Solution++;
printf("%d\n",Solution);
for(i=1; i<=N; i++)
if(Match_A[i]) printf("%d %d\n",i,Match_A[i]);
}