Pagini recente » Cod sursa (job #802216) | Cod sursa (job #1686462) | Cod sursa (job #2153202) | Cod sursa (job #1126499) | Cod sursa (job #1163392)
#include<stdio.h>
#include<vector>
using namespace std;
#define nmax 10005
int n, m, nrm, a, b, i, nrp, rez;
vector <int> ma[nmax];
int viz[nmax], st[nmax], dr[nmax];
bool sch;
void citire()
{
scanf("%ld %ld %ld",&n,&m,&nrm);
for (i=1;i<=nrm;i++)
{
scanf("%ld %ld",&a,&b);
ma[a].push_back(b);
}
}
bool pairup(int x)
{
vector <int> ::iterator it;
if (viz[x]==nrp)
return 0;
viz[x]=nrp;
for (it=ma[x].begin();it!=ma[x].end();it++)
if (dr[*it]==0)
{
st[x]=*it; dr[*it]=x;
return 1;
}
for (it=ma[x].begin();it!=ma[x].end();it++)
if (pairup(dr[*it]))
{
st[x]=*it; dr[*it]=x;
return 1;
}
return 0;
}
void rezolvare()
{
sch=1;
while (sch)
{
sch=0;
for (i=1;i<=n;i++)
if (st[i]==0)
{
nrp++;
if (pairup(i))
sch=1;
}
}
}
void afisare()
{
for (i=1;i<=n;i++)
rez+=(st[i]>0);
printf("%ld\n",rez);
for (i=1;i<=n;i++)
if (st[i]>0)
printf("%ld %ld\n",i,st[i]);
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
citire();
rezolvare();
afisare();
return 0;
}