Cod sursa(job #711258)

Utilizator mika17Mihai Alex Ionescu mika17 Data 11 martie 2012 19:54:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <vector>
#include <fstream>
#include <limits>
#include <algorithm>
using namespace std;

vector< vector<int > > GL, GR;
vector <int> lpair,rpair;
bool dfs();
int main() {

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

	int le,ri,e;
	in >> le >> ri >> e;
	
	GL.resize(le); GR.resize(ri);
	
	for(int x,y; e--;) {
		in >> x >> y;
		--x; --y;
		GL[x].push_back(y);
		GR[y].push_back(x);
	}

	lpair.assign(le,numeric_limits<int>::max()); rpair.assign(ri,numeric_limits<int>::max());
	
	while(dfs());
	
	out << lpair.size() - count(lpair.begin(),lpair.end(),numeric_limits<int>::max()) << endl;
	for(vector<int> :: iterator i = lpair.begin(); i != lpair.end(); ++i) {
		if(*i != numeric_limits<int>::max())
			out << i - lpair.begin() + 1 << ' ' << *i + 1 << endl;
	}
	return 0;
}

vector<bool> vis;

bool dfs_r(int k)
{
	if(k == numeric_limits<int>::max())
		return true;
	else
	{
		if(vis[k])
			return false;
		else
		{
			vis[k] = true;
			for(vector<int>::iterator i = GL[k].begin() ; i != GL[k].end(); ++i)
				if(dfs_r(rpair[*i]))
				{
					lpair[k] = *i;
					rpair[*i] = k;
					return true;
				}
			return false;
		}
	}
}

bool dfs() {
	
	bool found = false;

	vis.assign(lpair.size(),false);
	for(int i = 0; i < (int)lpair.size(); ++i)
		if(lpair[i] == numeric_limits<int>::max())
		{
			if(dfs_r(i))
				found = true;
		}
	return found;
}