Cod sursa(job #2841217)

Utilizator LXGALXGA a LXGA Data 29 ianuarie 2022 13:18:26
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>

using namespace std;
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");

int  n, m, e, x, y, mt[10001], st[10001];
bool use[10001], use2[10001];
vector<int> g[10001];
vector<pair<int, int>> ans;

bool matching(int nod)
{
	if (use[nod])
		return 0;
	use[nod] = 1;
	for (int &i : g[nod])
	{
		if (mt[i] == 0 || matching(mt[i]))
		{
			mt[i] = nod;
			st[nod] = i;
			return 1;
		}
	}
	return 0;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin >> n >> m >> e;
	for (int i = 1; i <= e; i++)
	{
		cin >> x >> y;
		g[x].push_back(y);
	}

	/*for (int i = 1; i <= n; i++)
	{
		for (auto j : g[i])
		{
			if (!mt[j])
			{
				mt[j] = i;
				use2[i] = 1;
				break;
			}
		}
	}
	for (int i = 1; i <= n; i++)
	{
		if (use2[i]) continue;
		for (int j = 1; j <= n; j++)
			use[j] = 0;
		matching(i);
	}*/
	for (int c = 1; c;)
	{
		c = 0;
		for (int j = 1; j <= n; j++)
			use[j] = 0;
		for (int i = 1; i <= n; i++)
		{
			if (!st[i])
				c |= matching(i);
		}
	}

	for (int i = 1; i <= m; i++)
	{
		if (mt[i])
			ans.push_back({ mt[i],i });
	}
	cout << ans.size() << '\n';
	for (auto i : ans)
		cout << i.first << ' ' << i.second << '\n';
	return 0;
}