Pagini recente » Cod sursa (job #1568774) | Cod sursa (job #2116341) | Cod sursa (job #2278478) | Cod sursa (job #1810409) | Cod sursa (job #771426)
Cod sursa(job #771426)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 10005
struct list{
int v;
struct list *n;
} *G[MAXN];
int left[MAXN], right[MAXN], L, R, nr;
char used[MAXN];
char matching(int i)
{
struct list *p;
if(used[i]) return 0;
used[i] = 1;
for(p = G[i]; p; p = p->n)
if(!right[p->v])
{
left[i] = p->v;
right[p->v] = i;
return 1;
}
for(p = G[i]; p; p = p->n)
if(matching(right[p->v]))
{
left[i] = p->v;
right[p->v] = i;
return 1;
}
return 0;
}
int main()
{
int i, a, b, sw;
struct list *p;
FILE *in = fopen("cuplaj.in", "r");
FILE *out = fopen("cuplaj.out", "w");
fscanf(in, "%d %d %d", &L, &R, &i);
for(; i; --i)
{
fscanf(in, "%d %d", &a, &b);
p = malloc(sizeof(struct list));
p->v = b;
p->n = G[a];
G[a] = p;
}
for(sw = 1; sw;)
{
memset(used, 0, sizeof(used));
for(sw = 0, i = 1; i <= L; ++i)
if( !left[i] )
if( matching(i) )
nr++,
sw = 1;
}
fprintf(out, "%d\n", nr);
for(i = 1; i <= L; ++i)
if( left[i] )
fprintf(out, "%d %d\n", i, left[i]);
fclose(in);
fclose(out);
return 0;
}