Cod sursa(job #2381946)

Utilizator mihai50000Mihai-Cristian Popescu mihai50000 Data 17 martie 2019 14:35:22
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

const int DIM = 1e4 + 7;

vector <int> v[DIM];

int M1[DIM];
int M2[DIM];

bitset <DIM> vis;

bool cuplaj(int nod)
{
	if(vis[nod] == 1)
		return false;
	
	vis[nod] = 1;
	
	for(auto i : v[nod])
		if(M2[i] == 0)
		{
			M1[nod] = i;
			M2[i] = nod;
			
			return true;
		}
		else
		{
			int x = M2[i];
			
			if(cuplaj(x))
			{
				M1[nod] = i;
				M2[i] = nod;
				return true;
			}
		}
	
	return false;
}

int main()
{
	int n, m, e;
	in >> n >> m >> e;
	
	while(e--)
	{
		int x, y;
		in >> x >> y;
		
		v[x].push_back(y);
	}
	
	bool ok = true;
	
	while(ok == true)
	{
		ok = false;
		
		vis.reset();
		
		for(int i = 1; i <= n; i++)
			if(M1[i] == 0 && cuplaj(i) == true)
			{
				ok = true;
			}
	}
	
	int nr = 0;
	
	for(int i = 1; i <= n; i++)
		if(M1[i] != 0)
			nr++;
	
	out << nr << '\n';
	
	for(int i = 1; i <= n; i++)
		if(M1[i] != 0)
			out << i << ' ' << M1[i] << '\n';
}