Cod sursa(job #1002254)

Utilizator roots4Irimia Alexandru Gabriel roots4 Data 27 septembrie 2013 10:17:12
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<fstream>
#include<vector>
#include<string.h>

#define max_n 10010

using namespace std;

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

int st[max_n] , dr[max_n];
int i , n , m , e , x , y , sol;
bool Viz[max_n] , ok = true;


vector<int>L[max_n];

void read(){
		
	f>>n>>m>>e;
	
	for( int i = 1 ; i <= e ; i++ ){
		f>>x>>y;
		L[x].push_back(y);
	}
	
}

bool dfs( int nod ){
	
	if( Viz[nod] )
		return 0;
		
	Viz[nod] = 1;
	
	for( int i = 0 ; i < L[nod].size() ; i++ ){
		if( dr[L[nod][i]] == 0 ){
			st[nod] = L[nod][i];
			dr[L[nod][i]] = nod;
			return 1;	
		}
	}
	
	for( int i = 0 ; i < L[nod].size() ; i++ ){
		if( dfs( dr[L[nod][i]] ) ){
			st[nod] = L[nod][i];
			dr[L[nod][i]] = nod;
			return 1;
		}
	}
	
	return 0;
}

void print(){

	for( int i = 1 ; i <= n ; i++ )
		if( st[i] )	sol++;
		
	g<<sol<<"\n";
	
	for( int i = 1 ; i <= n ; i++ )
		if( st[i] )
			g<<i<<" "<<st[i]<<"\n";
			
}

int main(){
	
	read();
	
	while( ok ){
		ok = 0;
		memset(Viz , 0 , sizeof(Viz));
		for(i = 1 ; i <= n ; i++) 
			if( st[i] == 0 )
				if( dfs(i) )
					ok = 1;
	}
	
	print();
	
	return 0;
}