Cod sursa(job #285158)

Utilizator ScrazyRobert Szasz Scrazy Data 22 martie 2009 13:38:02
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int maxn = 10001;

int n, m;
vector<int> g[maxn];

int viz[maxn];
int l[maxn], r[maxn]; //

int res = 0;

void read()
{ 
    int e;
    scanf("%d%d%d", &n, &m, &e);
    for (; e; --e)
    {
	int x, y; scanf("%d%d", &x, &y);
	g[x].push_back(y);
    }
}

int pairup(int x)
{ 
    if (viz[x]) return 0;
    viz[x] = 1;

    for (int i = 0; i < g[x].size(); ++i)
    {
	int next = g[x][i];
	if (r[next]) continue; 
	l[x] = next;
	r[next] = x;
	return 1;
    }

    for (int i = 0; i < g[x].size(); ++i)
    {
	int next = g[x][i];
	if (!pairup(r[next])) continue;

	l[x] = next;
	r[next] = x;
	return 1;
    } 

    return 0;
}

void cuplaj()
{ 
    int ok = 1;
    while(ok)
    {
	ok = 0;
	memset(viz, 0, sizeof(viz));
	for (int i = 1; i <= n; ++i)
	    if (!l[i])
	    if (pairup(i)) ok = 1;
    }
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);

    read();
    cuplaj();
    for (int i = 1; i <= n; ++i)
	if (l[i]) ++res;
    printf("%d\n", res);
    for (int i = 1; i <= n; ++i)
	if (l[i]) printf("%d %d\n", i, l[i]);

    return 0;
}