Cod sursa(job #2569291)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 4 martie 2020 11:45:34
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 1e4 + 1;

bitset <DIM> vis;
vector <int> adj[DIM];
int M1[DIM];
int M2[DIM];

bool cuplaj(int nod)
{
	if(vis[nod])
		return false;
	
	vis[nod] = true;
	
	for(auto i : adj[nod])
		if(!M2[i])
		{
			M2[i] = nod;
			M1[nod] = i;
			return true;
		}
	
	for(auto i : adj[nod])
		if(cuplaj(M2[i]))
		{
			M1[nod] = i;
			M2[i] = nod;
			return true;
		}
	
	return false;
}

main()
{
	int n, m, k;
	fin >> n >> m >> k;
	
	for(; k; --k)
	{
		int x, y;
		fin >> x >> y;
		
		adj[x].emplace_back(y);
	}
	
	bool ok = true;
	
	while(ok)
	{
		ok = false;
		
		vis.reset();
		
		for(int i = 1; i <= n; ++i)
			if(!M1[i])
			{
				ok |= cuplaj(i);
			}
	}
	
	int ans = 0;
	
	for(int i = 1; i <= n; ++i)
		if(M1[i])
			++ans;
	
	fout << ans << '\n';
	
	for(int i = 1; i <= n; ++i)
		if(M1[i])
			fout << i << ' ' << M1[i] << '\n';
}