Cod sursa(job #3320299)

Utilizator Ruxandra009Ruxandra Vasilescu Ruxandra009 Data 4 noiembrie 2025 20:27:09
Problema Cuplaj maxim in graf bipartit Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

const string txt = "cuplaj";
const int nmax = 1e4 + 5;

ifstream f(txt + ".in");
ofstream g(txt + ".out");

int n1, n2, m, mat[nmax], viz[nmax];
vector<int> v[nmax];

static bool mbm(int node, int z)
{
	for (auto x : v[node])
	{
		if (viz[x] == z) continue;
		viz[x] = z;

		if (!mat[x] || mbm(mat[x], z)) {
			mat[x] = node;
			
			return true;
		}
	}

	return false;
}

static int cuplaj_max()
{
	int ans = 0;
	for (int i = 1; i <= n2; i++)
		if (mbm(i, i)) ans++;

	return ans;
}

int main()
{
	f.tie(NULL);
	f >> n1 >> n2 >> m;
	for (int i = 1; i <= m; i++)
	{
		int x, y; f >> x >> y;
		v[y].push_back(x);
	}

	g << cuplaj_max() << '\n';
	for (int i = 1; i <= n1; i++)
		if (mat[i]) g << i << " " << mat[i] << '\n';
	return 0;
}