Pagini recente » Cod sursa (job #3120521) | Cod sursa (job #66572) | Cod sursa (job #3133962) | Cod sursa (job #2347326) | Cod sursa (job #771405)
Cod sursa(job #771405)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 10000
struct list{
short v;
struct list *n;
} *G[MAXN];
short left[MAXN], right[MAXN], L, R, nr;
char used[MAXN];
int 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] || matching(right[p->v]))
{
left[i] = p->v;
right[p->v] = i;
return 1;
}
return 0;
}
int main()
{
short i, a, b;
struct list *p;
FILE *in = fopen("cuplaj.in", "r");
FILE *out = fopen("cuplaj.out", "w");
fscanf(in, "%hd %hd %hd", &L, &R, &i);
for(; i; --i)
{
fscanf(in, "%hd %hd", &a, &b);
p = malloc(sizeof(struct list));
p->v = b;
p->n = G[a];
G[a] = p;
}
for(i = 1; i <= L; ++i)
{
if( left[i] )
continue;
else
{
memset(used, 0, sizeof(char)*(L+1));
if( matching(i) )
nr++;
}
}
fprintf(out, "%hd\n", nr);
for(i = 1; i <= L; ++i)
if( left[i] )
fprintf(out, "%hd %hd\n", i, left[i]);
fclose(in);
fclose(out);
return 0;
}