Pagini recente » Cod sursa (job #823852) | Cod sursa (job #623322) | Cod sursa (job #448535) | Cod sursa (job #1817332) | Cod sursa (job #2419131)
#include <bits/stdc++.h>
#define nmax 10005
#define pb push_back
using namespace std;
int n, m, e, x, y;
vector<int>g[nmax];
int st[nmax], dr[nmax], ans;
bool viz[nmax];
bool cup(int nod)
{
if (viz[nod]) return 0;
viz[nod] = true;
for (auto x:g[nod])
{
if (!dr[x])
{
st[nod] = x;
dr[x] = nod;
return true;
}
}
for (auto x:g[nod])
{
if (cup(dr[x]))
{
st[nod] = x;
dr[x] = nod;
return true;
}
}
return false;
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d %d %d",&n,&m,&e);
for (int i=1; i<=e; ++i)
{
scanf("%d %d",&x,&y);
g[x].pb(y);
}
bool ok = true;
while(ok)
{
ok = false;
memset(viz,0,sizeof(viz));
for (int i=1; i<=n; ++i)
if (!st[i] && !viz[i])
{
if (cup(i))
ok = true;
}
}
for (int i=1; i<=n; ++i)
if (st[i])
++ans;
printf("%d\n",ans);
for (int i=1; i<=n; ++i)
if (st[i])
printf("%d %d\n",i,st[i]);
fclose(stdin);
fclose(stdout);
return 0;
}