Cod sursa(job #750412)

Utilizator Marius_mFMI-M2 Marius Melemciuc Marius_m Data 22 mai 2012 02:53:32
Problema Cuplaj maxim in graf bipartit Scor 28
Compilator cpp Status done
Runda Arhiva educationala Marime 1.19 kb
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

const int Nmax = 10001;

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

vector<int> graf[Nmax];
int N,M,E,layer[Nmax],nr_muchii,conect[Nmax];
int viz[Nmax];

void initializare()
{
	for(int i = 1 ; i <= N ; i++)
		viz[i] = 0;
}

int cuplaj(int nod)
{
	if( viz[nod] == 1 )
		return 0;
	viz[nod] = 1;
	int k;
	for( k = 0 ; k < graf[nod].size() ; k++ )
		if( !conect[ graf[nod][k] ] )
		{
			layer[nod] = graf[nod][k];
			conect[graf[nod][k]] = nod;
			return 1;
		}
	for( k = 0 ; k < graf[nod].size(); k++ )
		if( cuplaj(graf[nod][k]) )
		{
			layer[nod] = graf[nod][k];
			conect[graf[nod][k]] = nod;
			return 1;
		}
	return 0;
}

int main()
{
	in>>N>>M>>E;
	int i,x,y;
	for( i = 1 ; i <= E ; i++ )
	{
		in>>x>>y;
		graf[x].push_back(y);
	}
	bool pos=1;
	while( pos == 1 )
	{
		pos = 0;
		initializare();
		for( i = 1 ; i <= N ; i++ )
			if( layer[i]==0 && cuplaj(i) == 1 )
			{
				pos = 1;
				nr_muchii++;
			}
	}
	out<<nr_muchii<<"\n";
	for( i = 1 ; i<= N ; i++ )
		if( layer[i]!=0 )
		out<<i<<" "<<layer[i]<<"\n";
	in.close();
	out.close();
	return 0;
}