Pagini recente » Cod sursa (job #1225675) | Cod sursa (job #2933807) | Cod sursa (job #829924) | Cod sursa (job #2869798) | Cod sursa (job #582808)
Cod sursa(job #582808)
#include <cstdio>
#include <cstring>
#include <fstream>
#define Nmx 10001
using namespace std;
int L[Nmx],R[Nmx],viz[Nmx],n,m;
struct nod
{
int inf;
nod *urm;
};
nod *G[Nmx];
ifstream fin("cuplaj.in");
void add(int x,int y)
{
nod *aux=new nod;
aux->inf = y;
aux->urm = G[x];
G[x] = aux;
}
void read()
{
int t,x,y;
fin>>n>>m>>t;
for(;t;t--)
{
fin>>x>>y;
add(x,y);
}
}
int pairup(int x)
{
if(viz[x])
return 0;
viz[x]=1;
for(nod *p=G[x];p;p=p->urm)
if(!R[p->inf])
{
R[p->inf]=x;
L[x]=p->inf;
return 1;
}
for(nod *p=G[x];p;p=p->urm)
if(pairup(R[p->inf]))
{
R[p->inf]=x;
L[x]=p->inf;
return 1;
}
return 0;
}
void solve()
{
int ok,cuplaj=0;
do
{
ok=0;
memset(viz,0,sizeof(viz));
for(int i=1;i<=n;++i)
if(!L[i]&&pairup(i))
{
cuplaj++;
ok=1;
}
}while(ok);
printf("%d\n",cuplaj);
for(int i=1;i<=n;++i)
if(L[i])
printf("%d %d\n",i,L[i]);
}
int main()
{
freopen("cuplaj.out","w",stdout);
read();
solve();
return 0;
}