Cod sursa(job #1465037)

Utilizator greenadexIulia Harasim greenadex Data 26 iulie 2015 13:38:56
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define s second

using namespace std;

const string name = "cuplaj",
			 in_file = name + ".in", 
			 out_file = name + ".out";

ifstream fin(in_file);
ofstream fout(out_file);

const int MAX = 1e4 + 1;
int n, m, e, l[MAX], r[MAX], sol, viz[MAX];
vector<int> v[MAX];

int pair_up(int x){
	if (viz[x])
		return 0;
	else viz[x] = 1;

	for (auto it: v[x])
		if (r[it] == 0){
			l[x] = it;
			r[it] = x;
			return 1;
		}else if (pair_up(r[it])){
				l[x] = it;
				r[it] = x;
				return 1;
			}

	return 0;
}

int main(){
	fin >> n >> m >> e;
	for (int a, b; e; e--){
		fin >> a >> b;
		v[a].pb(b);
	}

	for (int ok = 1; ok;){
		ok = 0;
		memset(viz, 0, sizeof(viz));
		for (int i = 1; i <= n; i++)
			if (!l[i] && pair_up(i)){
				ok = 1;
				sol++;
			}
	}

	fout << sol << '\n';

	for (int i = 1; i <= n; i++)
		if(l[i])
			fout << i << ' ' << l[i] << '\n';

	return 0;
}