Cod sursa(job #873817)

Utilizator fhandreiAndrei Hareza fhandrei Data 7 februarie 2013 17:43:21
Problema Cuplaj maxim in graf bipartit Scor 32
Compilator cpp Status done
Runda Arhiva educationala Marime 2.94 kb
// Include
#include <fstream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

// Definitii
#define pb push_back
#define mp make_pair

#define edge pair<int, int>
#define from first
#define to second

// Consatante
//const int sz = (int)2e4+3;
const int sz = 2001;
const int oo = (1<<30)-1;

const int source = 0;

// Functii
bool bfs();

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

int leftNodes, rightNodes, edges;
int destination;
int maxFlow;

int father[sz];
int position[sz];
short int flow[sz][sz];
bool capacity[sz][sz];

vector<int> graph[sz];

vector<edge> answer;

// Main
int main()
{
	in >> leftNodes >> rightNodes >> edges;
	destination = leftNodes + rightNodes + 1;
	
	for(int i=1 ; i<=leftNodes ; ++i)
	{
		graph[source].pb(i);
		graph[i].pb(source);
		capacity[source][i] = 1;
	}
	
	for(int i=leftNodes+1 ; i<destination ; ++i)
	{
		graph[i].pb(destination);
		graph[destination].pb(i);
		capacity[i][destination] = 1;
	}
	
	int rFrom, rTo;
	while(edges--)
	{
		in >> rFrom >> rTo;
		
		graph[rFrom].pb(rTo+=leftNodes);
		graph[rTo].pb(rFrom);
		
		capacity[rFrom][rTo] = 1;
	}
	
	while(bfs())
	{
		vector<int>::iterator it, end=graph[destination].end();
		for(it=graph[destination].begin() ; it!=end ; ++it)
		{
			if(father[*it] == -1 || capacity[*it][destination] == flow[*it][destination])
				continue;
			
			father[destination] = *it;
			
			int minFlow = oo;
			
			for(int node = destination ; node!=source ; node=father[node])
				minFlow = min(minFlow, capacity[father[node]][node] - flow[father[node]][node]);
			
			if(!minFlow)
				continue;
			
			int leftNode, rightNode;
			for(int node=destination ; node!=source ; node=father[node])
			{
				rightNode = leftNode;
				leftNode = node;
				
				++flow[father[node]][node];
				--flow[node][father[node]];
			}
			
			++maxFlow;
			if(position[rightNode])
			{
				answer[position[rightNode]-1].to = *it-leftNodes;
				position[*it] = position[rightNode];
			}
			position[rightNode] = answer.size()+1;
			answer.pb(mp(leftNode, rightNode-leftNodes));
		}
	}
	
	out << maxFlow << '\n';
	
	vector<edge>::iterator it, end=answer.end();
	
	for(it=answer.begin() ; it!=end ; ++it)
		out << it->from << ' ' << it->to << '\n';
	
	in.close();
	out.close();
	return 0;
}

bool bfs()
{
	memset(father, -1, sizeof(father));
	
	queue<int> q;
	
	q.push(source);
	father[source] = -2;
	
	while(!q.empty())
	{
		int node = q.front();
		q.pop();
		
		if(node == destination)
			break;
		
		vector<int>::iterator it, end=graph[node].end();
		
		for(it=graph[node].begin() ; it!=end ; ++it)
		{
			if(father[*it] != -1) // Daca e vizitat
				continue;
			if(flow[node][*it] != capacity[node][*it])
			{
				father[*it] = node;
				q.push(*it);
			}
		}
	}
	
	return father[destination] == -1? false : true;
}