Pagini recente » Cod sursa (job #40680) | Cod sursa (job #1791339) | Cod sursa (job #3245439) | Cod sursa (job #2928176) | Cod sursa (job #1517892)
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int nmax = 10000;
vector <int> g[nmax+5];
bool viz[nmax+5];
int n, m, l[nmax+5], r[nmax+5];
int cup(int x)
{
if(viz[x])return 0;
viz[x] = true;
vector <int> :: iterator it = g[x].begin();
while(it!=g[x].end())
{
if(!r[*it])
{
l[x] = *it;
r[*it] = x;
return 1;
}
it++;
}
it = g[x].begin();
while(it!=g[x].end())
{
if(cup(r[*it]))
{
l[x] = *it;
r[*it] = x;
return 1;
}
it++;
}
return 0;
}
int main()
{
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
int e;
scanf("%d%d%d", &n, &m, &e);
for(int i=0; i<e; i++)
{
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
}
bool ok = true;
while(ok)
{
ok = false;
memset(viz, 0, sizeof(viz));
for(int i=1; i<=n; i++)
if(!l[i] && cup(i))
ok = true;
}
int cuplaj = 0;
for(int i=1; i<=n; i++)
if(l[i])cuplaj++;
printf("%d\n", cuplaj);
for(int i=1; i<=n; i++)
if(l[i])printf("%d %d\n", i, l[i]);
return 0;
}