Pagini recente » Cod sursa (job #2960726) | Cod sursa (job #2945505) | Cod sursa (job #1162987) | Cod sursa (job #2723593)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int N = 10005;
int n,m,e,L[N],R[N],cp;
bool seen[N];
vector <int> G[N];
void Read()
{
fin>>n>>m>>e;
while(e--)
{
int x,y;
fin>>x>>y;
G[x].push_back(y);
}
}
int PairUP(int x)
{
if(seen[x])
return 0;
seen[x]=1;
vector <int> ::iterator it;
for(it=G[x].begin(); it!=G[x].end(); it++)
{
if(!R[*it] || PairUP(R[*it]))
{
L[x]=*it;
R[*it]=x;
return 1;
}
}
return 0;
}
void Cuplaj()
{
int ok=1;
while(ok)
{
memset(seen,0,sizeof(bool)*(n+1));
ok=0;
for(int i=1; i<=n; i++)
{
if(!L[i])
{
if(PairUP(i))
{
cp++;
ok=1;
}
}
}
}
}
void Print()
{
fout<<cp<<'\n';
for(int i=1; i<=n; i++)
{
if(L[i])
fout<<i<<" "<<L[i]<<'\n';
}
}
int main()
{
Read();
Cuplaj();
Print();
}