Cod sursa(job #1956413)

Utilizator bt.panteaPantea Beniamin bt.pantea Data 6 aprilie 2017 18:50:28
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
// Cuplaj.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#include <bitset>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
const int NMAX = 10005;
int n, m, e;
vector < int > a[NMAX];
bitset < NMAX > b;
int Father[NMAX], urm[NMAX];
bool pairup(const int & nod) {
	if (b[nod]) return false;
	b[nod] = 1;
	for (vector < int >::iterator it = a[nod].begin(); it != a[nod].end(); it++)
		if (Father[*it] == 0) {
			Father[*it] = nod;
			urm[nod] = *it;
			return true;
		}
	for (vector < int >::iterator it = a[nod].begin(); it != a[nod].end(); it++)
		if (pairup(Father[*it])) {
			Father[*it] = nod;
			urm[nod] = *it;
			return true;
		}
	return false;
}

int main()
{
	f >> n >> m >> e;
	while (e--) {
		int x, y;
		f >> x >> y;
		a[x].push_back(y);
	}
	bool fin;
	do {
		fin = false;
		b = bitset < NMAX >(0);
		for (int i = 1; i <= n; i++) if (urm[i] == 0)
			fin |= pairup(i);
		//cout << "totdeauna boss\n";
	} while (fin);

	vector < pair < int, int > > sol;
	for (int i = 1; i <= n; i++)
		if (urm[i] != 0) sol.push_back(make_pair(i, urm[i]));
	g << sol.size() << '\n';
	for (int i = 0; i < sol.size(); i++)
		g << sol[i].first << ' ' << sol[i].second << '\n';
    return 0;
}