Cod sursa(job #1700739)

Utilizator mihai995mihai995 mihai995 Data 11 mai 2016 07:54:33
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <cstring>
#include <vector>
using namespace std;

const int N = 1 + 1e5;

vector<int> adj[N];
int match[N], pairof[N], nA, nB;
bool use[N];


bool pairup(int x){
	if ( use[x] ) return false;
	use[x] = true;

	for (int y : adj[x])
		if ( pairof[y] == -1 ){
			match[x] = y;
			pairof[y] = x;
			return true;
		}
	for (int y : adj[x])
		if ( pairup( pairof[y] ) ){
			match[x] = y;
			pairof[y] = x;
			return true;
		}
	return false;
}
int maxMatch(){
	memset(match, -1, sizeof(match));
	memset(pairof, -1, sizeof(pairof));
	bool go;
	do {
		memset(use, false, sizeof(use));
		go = false;
		for (int i = 1; i <= nA; i++)
			if ( match[i] == -1 )
				go |= pairup(i);
	} while (go);
}

int main(){
	ifstream in("cuplaj.in");
	ofstream out("cuplaj.out");
	int times, x, y;

	in >> nA >> nB >> times;
	while (times--){
		in >> x >> y;
		adj[x].push_back(y);
	}

	maxMatch();
	int ans = 0;
	for (int i = 1; i <= nA; i++)
		ans += match[i] != -1;
	out << ans << '\n';

	for (int i = 1; i <= nA; i++)
		if ( match[i] != -1 )
			out << i << ' ' << match[i] << '\n';
	return 0;
}