Cod sursa(job #877980)

Utilizator fhandreiAndrei Hareza fhandrei Data 13 februarie 2013 17:12:37
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
// Include
#include <fstream>
#include <vector>
using namespace std;

// Definitii
#define pb push_back

// Constante
const int sz = (int)1e5+8;

// Functii
int link(int node);

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

int leftNodes, rightNodes, edges;
int Left[sz], Right[sz];
bool Fixed[sz];

int answer;
vector<int> graph[sz];

// Main
int main()
{
	in >> leftNodes >> rightNodes >> edges;
	int rFrom, rTo;
	while(edges--)
	{
		in >> rFrom >> rTo;
		graph[rFrom].pb(rTo);
	}
	
	for(int i=1 ; i<=leftNodes ; ++i)
	{
		if(!link(i))
		{
			memset(Fixed, false, (leftNodes+1)*sizeof(bool));
			answer += link(i);
		}
		else
			++answer;
	}
	
	out << answer << '\n';
	for(int i=1 ; i<=leftNodes ; ++i)
		if(Right[i])
			out << i << ' ' << Right[i] << '\n';
	
	in.close();
	out.close();
	return 0;
}

int link(int node)
{
	if(Fixed[node])
		return 0;
	Fixed[node] = true;
	
	vector<int>::iterator it, end=graph[node].end();
	for(it=graph[node].begin() ; it!=end ; ++it)
	{
		if(!Left[*it])
		{
			Left[*it] = node;
			Right[node] = *it;
			return 1;
		}
	}
	
	for(it=graph[node].begin() ; it!=end ; ++it)
	{
		if(link(Left[*it]))
		{
			Left[*it] = node;
			Right[node] = *it;
			return 1;
		}
	}
	
	return 0;
}