Cod sursa(job #877950)

Utilizator fhandreiAndrei Hareza fhandrei Data 13 februarie 2013 15:58:36
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 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];

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;
	}
	
	
	
	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(!Left[*it])
		{
			Left[*it] = node;
			Right[node] = *it;
			return 1;
		}
	}
	
	return 0;
}