Pagini recente » Cod sursa (job #1596759) | Borderou de evaluare (job #1784105) | Cod sursa (job #743457) | Cod sursa (job #2978842) | Cod sursa (job #1391321)
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX_N = 10000, MAX_M = 10000;
FILE *in, *out;
int lst[MAX_N+1], urm[MAX_N*MAX_M+1], vf[MAX_N*MAX_M+1];
int nrg;
void add(int x, int y)
{
nrg++;
urm[nrg] = lst[x];
vf[nrg] = y;
lst[x] = nrg;
}
int left[MAX_N+1], right[MAX_M+1];
bool u[MAX_N+1];
bool makepair(int nod)
{
if(u[nod] == true) return false;
u[nod] = true;
int p = lst[nod];
while(p != 0)
{
if(right[vf[p]] == 0)
{
left[nod] = vf[p];
right[vf[p]] = nod;
return true;
}
p = urm[p];
}
p = lst[nod];
while(p != 0)
{
if(makepair(right[vf[p]]))
{
left[nod] = vf[p];
right[vf[p]] = nod;
return true;
}
p = urm[p];
}
return false;
}
int main()
{
in = fopen("cuplaj.in", "r");
out = fopen("cuplaj.out", "w");
int n, m, e;
int x, y;
fscanf(in, "%d%d%d", &n, &m, &e);
for(int i = 0; i < e; i++)
{
fscanf(in, "%d%d", &x, &y);
add(x,y);
}
bool nostop = true;
while(nostop)
{
nostop = false;
memset(u, false, sizeof(u));
for(int i = 1; i <= n; i++)
if(left[i] == 0)
nostop |= makepair(i);
}
int cup = 0;
for(int i = 1; i <= n; i++)
cup += (left[i] != 0);
fprintf(out, "%d\n", cup);
for(int i = 1; i <= n; i++)
if(left[i] != 0)
fprintf(out, "%d %d\n", i, left[i]);
fclose(in);
fclose(out);
return 0;
}