Pagini recente » Cod sursa (job #1405152) | Cod sursa (job #2551872) | Cod sursa (job #1161787) | Cod sursa (job #2635977) | Cod sursa (job #404437)
Cod sursa(job #404437)
#include<cstdio>
#include<bitset>
using namespace std;
#define NMAX 10005
#define sh short int
sh N,M,x,y,dr[NMAX],st[NMAX];
int nr,E;
bitset<NMAX>a[NMAX];
bitset<NMAX>viz;
bool df (sh n)
{
if (viz[n])
return 0;
viz[n]=1;
for (sh i=1; i<=M; ++i)
{
if (!a[n][i])
continue;
if (!dr[i] || df(dr[i]) )
{
st[n]=i;
dr[i]=n;
return 1;
}
}
return 0;
}
void cuplaj()
{
sh i;
for (i=1; i<=N; ++i)
{
if (st[i])
continue;
if (df(i))
++nr;
else
{
viz.reset();
if (df(i))
++nr;
}
}
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%hd%hd%d",&N,&M,&E);
int i;
for (i=1; i<=E; ++i)
{
scanf("%hd%hd",&x,&y);
a[x][y]=1;
}
cuplaj();
printf("%d\n",nr);
for (int i=1;i<=N||i<=M; ++i)
{
if (st[i]&&dr[st[i]])
printf("%hd %hd\n",dr[st[i]],st[i]);
}
return 0;
}