Cod sursa(job #870969)

Utilizator milijrCristian Militaru milijr Data 4 februarie 2013 10:38:45
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <stdio.h>
#include <vector>
#include <bitset>

using namespace std;

#define MAXN 10005

FILE *in = fopen("cuplaj.in", "r");
FILE *out= fopen("cuplaj.out","w");

vector<int> graf[MAXN];
int n, m, fr[MAXN], e;
int coresp[MAXN];
bitset<MAXN> folosit;
int gasite;

void gaseste_coresp(int indice)
{
	vector<int>::iterator it;
	int i, min = 100000;
	int ind_min = 0;
	for(it = graf[indice].begin(), i = 1; it != graf[indice].end(); i++, it++)
	{
		if(fr[*it] < min && folosit[*it] == 0)
		{
			min = fr[*it];
			ind_min = *it;
		}
	}
	if(ind_min != 0)
	{
		folosit[ind_min] = 1;
		coresp[indice] = ind_min;
		gasite++;
	}
	else
	{
		coresp[indice] = -1; // nu s-a gasit corespondent
	}
	for(it = graf[indice].begin(), i = 1; it != graf[indice].end(); i++, it++)
		if(folosit[*it] == 0)
			fr[*it]--;
}

int main()
{
	int i, x, y;
	fscanf(in, "%i %i %i", &n, &m, &e);
	
	for(i = 1; i <= e; i++)
	{
		fscanf(in, "%i %i", &x, &y);
		fr[y] ++;
		graf[x].push_back(y);
	}
	
	folosit.reset();
	
	for(i = 1; i <= n; i++)
	{
		gaseste_coresp(i);
	}
	
	fprintf(out, "%i\n", gasite);
	
	for(i = 1; i <= n; i++)
		if(coresp[i] != -1)
			fprintf(out, "%i %i\n", i, coresp[i]);
}