Cod sursa(job #2034083)

Utilizator gabib97Gabriel Boroghina gabib97 Data 7 octombrie 2017 13:58:33
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>
#define N 10005

using namespace std;

int n, m, e, p1[N], p2[N];
bool o[N];
vector<int> G[N];

int match(int s)
{
	if (o[s]) return 0;
	o[s] = 1;

	for (int x: G[s])
		if (!p2[x])
		{
			p1[s] = x;
			p2[x] = s;
			return 1;
		}

	for (int x: G[s])
		if (match(p2[x]))
		{
			p1[s] = x;
			p2[x] = s;
			return 1;
		}

	return 0;
}

int main()
{
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	scanf("%i%i%i", &n, &m, &e);
	
	int x, y, i;
	while (e--)
	{
		scanf("%i%i", &x, &y);
		G[x].push_back(y);
	}

	int cuplaj;
	do 
	{
		cuplaj = 0;
		memset(o, 0, sizeof(o));

		for (i = 1; i <= n; i++)
			if (!p1[i]) cuplaj += match(i);
	} while (cuplaj);

	cuplaj = 0;
	for (i = 1; i <= n; i++)
		if (p1[i]) cuplaj++;

	printf("%i\n", cuplaj);
	for (i = 1; i <= n; i++)
		if (p1[i]) printf("%i %i\n", i, p1[i]);
	return 0;
}