Cod sursa(job #2279590)

Utilizator marcudanfDaniel Marcu marcudanf Data 9 noiembrie 2018 19:32:50
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
#include <vector>

const int NMAX = 1e4 + 5;

using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

vector < int > g[NMAX];

int n, m, e, s;
int l[NMAX], r[NMAX], viz[NMAX];

bool pairup(int nod){
	if(viz[nod])
		return 0;
	viz[nod] = 1;
	for(auto i : g[nod])
		if(!r[i]){
			l[nod] = i;
			r[i] = nod;
			return 1;
		}
	for(auto i : g[nod])
		if(pairup(r[i])){
			l[nod] = i;
			r[i] = nod;
			return 1;
		}
	return 0;
}

int main(){
	fin >> n >> m >> e;
	while(e--){
		int a, b;
		fin >> a >> b;
		g[a].push_back(b);
	}
	bool change = 1;
	while(change){
		change = 0;
		for(int i = 0 ; i <= n; i++)
			viz[i] = 0;
		for(int i = 1; i <= n; i++)
			if(!l[i])
				change = change | pairup(i);
	}
	for(int i = 1; i <= n; i++)
		s += (l[i] != 0);
	fout << s << '\n';
	for(int i = 1; i <= n; i++)
		if(l[i])
			fout << i << ' ' << l[i] << '\n';
	return 0;
}