Cod sursa(job #2647217)

Utilizator BereaBerendea Andrei Berea Data 3 septembrie 2020 16:18:35
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>

using namespace std;

int n,m,e,x,y,ok;
const int MAXN=10001;
vector<int>g[MAXN];
int cnt; // raspunsul cate muchii are cuplajul
int L[MAXN];
//L[st] = cu cine l-am cuplat in dreapta
int R[MAXN];
int viz[MAXN];

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

bool cuplaj(int x) { // imi spune pentru un nod din stanga daca il pot cupla
	if(viz[x] == 1) // am mai fost prin nodul asta si era useless
		return false;
	viz[x] = true;
	for ( auto y: g[x])
		if(!R[y] or cuplaj(R[y]) == true) {
		L[x] = y;
R[y] = x;
return true;

}
return false;
}

int main(){
fin >> n >> m >> e;
for ( int i = 1; i <= e; ++i) {
	fin >> x >> y;
	g[x].push_back(y+n);
}
	ok = true;
	while(ok == true) {
		ok = false;
		memset(viz,0,sizeof(viz));
for ( int i = 1; i <= n; ++i)
			if(!L[i] and cuplaj(i) == true)
				{
				++cnt;
				ok = true;
}
}

fout << cnt<<"\n";
for ( int i =1 ; i <= n; ++i)
	if(L[i])
		fout << i << " " << L[i]-n << "\n";
}