Cod sursa(job #563807)

Utilizator angelaAngela Visuian angela Data 26 martie 2011 07:59:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
#define DIM 10001

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

typedef vector<int> VI;
typedef VI::iterator IT;

VI G[DIM];
int n, m, K;   // noduri din prima si a doua pratitie, nr de muchii
int v[DIM];   // vizitat
VI L, R;

void Read();
bool Cupleaza(int x);

int main()
{
	Read();
	int cuplaj = 0;
	bool am_cuplaj = false; 
	do 
	{
		memset(v, false, sizeof(v));
		am_cuplaj = false;
		for ( int i = 1; i <= n; ++i )
			if ( !L[i] && Cupleaza(i) )
			{
				cuplaj ++;
				am_cuplaj = true;
			}
	} while ( am_cuplaj );
	
	fout << cuplaj << '\n';
	for ( int i = 1; i <= n; ++i )
		if ( L[i] )
			fout << i << ' ' << L[i] << '\n';
	
	fout.close();
}


bool Cupleaza(int x)
{
	if ( v[x] ) return false;
	v[x] = true;
	for ( IT i = G[x].begin(); i != G[x].end(); ++i )
		if ( !R[*i] || Cupleaza(R[*i]) )
		{
			L[x] = *i;
			R[*i] = x;
			return true;
		}
	return false;
}

void Read()
{
	fin >> n >> m >> K;
	L.resize(n + 1), R.resize(m + 1);
	int x, y;
	for ( int i = 0; i < K; ++i )
	{
		fin >> x >> y;
		G[x].push_back(y);
	}
	fin.close();
}