Cod sursa(job #939347)

Utilizator bzxbzxbzxGhiuzan Paul bzxbzxbzx Data 14 aprilie 2013 20:11:16
Problema Cuplaj maxim in graf bipartit Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int>* vec;
int *st, *dr, *viz;
int s, d, e, n;

void citeste ( const char file[] ) {
	ifstream in;
	in.open ( file );
	in >> s >> d >> e;
	vec = new vector<int> [ s+d+1 ];
	int i, a, b;
	for ( i = 1; i <= e; i++ ) {
		in >> a >> b;
		vec[a].push_back ( b );
	}
	in.close();
}

int cupleaza ( int x ) {
	if ( viz[x] == 1 )
		return 0;
	viz[x] = 1;
	int i;
	for ( i = 0; i < vec[x].size(); i++ )
		if ( dr[vec[x][i]] == 0 ) {
			st[x] = vec[x][i];
			dr[vec[x][i]] = x;
			return 1;
		}
	for ( i = 0; i < vec[x].size(); i++ )
		if ( cupleaza ( dr[vec[x][i]] ) == 1 ) {
			st[x] = vec[x][i];
			dr[vec[x][i]] = x;
			return 1;
		}
	return 0;
}

void cuplajMax( const char file[] ) {
	int ok = 1, x = 0, i;
	while ( ok == 1 ) {
		ok = 0;
		for ( i = 1; i <= s; i++ )
			viz[i] = 0;
		for ( i = 1; i <= s+d; i++ )
			if ( st[i] == 0 )
				if ( cupleaza (i) == 1 )
					ok = 1;
	}
	ofstream out;
	out.open ( file );
	for ( i = 1; i <= s; i++ )
		if ( st[i] > 0 )
			x++;
	out << x << '\n';
	for ( i = 1; i <= s; i++ )
		if ( st[i] != 0 )
			out << i << ' ' << st[i] << '\n';
	out.close();
}

int main() {
	citeste( "cuplaj.in" );
	st = new int [s+1];
	dr = new int [d+1];
	viz = new int [s+1];
	int i;
	for ( i = 1; i <= s; i++ ) {
		st[i] = 0;
		viz[i] = 0;
	}
	for ( i = 1; i <= d; i++ )
		dr[i] = 0;
	cuplajMax( "cuplaj.out" );
	return 0;
}