Cod sursa(job #301388)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 8 aprilie 2009 10:15:07
Problema Cuplaj maxim in graf bipartit Scor 18
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
// cuplaj.cpp : Defines the entry point for the console application.
//


#include <cstdio>
#include <bitset>
#include <vector>

using namespace std;

#define maxN	10010
#define pb		push_back

bitset <maxN> viz;
vector <int> G[maxN];
int cuplaj[maxN], cuplaj2[maxN], N, M, E, Sol;

bool df (int nod) {
	if (viz[nod])	return false;
	viz[nod] = 1;
	int vecin;
	
	for (int i = 0; i < G[nod].size(); ++ i) {
		vecin = G[nod][i];
		if (! cuplaj[vecin]) {
			cuplaj[vecin] = nod;
			return true;
		}
	}

	for (int i = 0; i < G[nod].size(); ++ i) {
		vecin = G[nod][i];
		if (cuplaj[vecin] != nod && df (cuplaj[vecin])) {
			cuplaj[vecin] = nod;
			return true;
		}
	}
	
	return false;
}

void print () {
	int i;
		
	printf("%d\n", Sol);
	for (i = 1; i <= N; ++ i)
		if (cuplaj[i])
			printf("%d %d\n", cuplaj[i], i);

}

int main( )
{
	int a, b, i, ok = 1;
	
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);

	scanf("%d%d%d", &N, &M, &E);

	for ( ; E -- ; ) {
		scanf("%d%d", &a, &b);
		G[a].pb(b);
	}

	ok = 1;
	
	while (ok) {
		ok = 0;
		viz.reset();
		for (i = 1; i <= N; ++ i)
			if (df(i)) {
				Sol ++;
				ok = 1;
			}
	}
	print ();
	return 0;
}