Cod sursa(job #2848090)

Utilizator alextmAlexandru Toma alextm Data 12 februarie 2022 09:50:03
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 10001;

vector<int> G[MAXN];
int st[MAXN], dr[MAXN];
bool viz[MAXN];

bool Cupleaza(int node) {
	if(viz[node] != 0) return 0;
	viz[node] = 1;
	
	for(auto nxt : G[node]) {
		if(!st[nxt] || Cupleaza(st[nxt])) {
			st[nxt] = node;
			dr[node] = nxt;
			return 1; // am putut cupla
		}
	}
	
	return 0;
}

int main() {
	int n, m, e, x, y;
	fin >> n >> m >> e;
	
	while(e--) {
		fin >> x >> y;
		G[x].push_back(y);
	}
	
	int found = 1;
	while(found) {
		memset(viz, 0, sizeof(viz));
		found = 0;
		for(int i = 1; i <= n && !found; i++)
			if(!dr[i] && Cupleaza(i))
				found = 1;
	}
	
	int nr = 0;
	for(int i = 1; i <= n; i++)
		nr += (dr[i] != 0);
		
	fout << nr << '\n';
	for(int i = 1; i <= n; i++)
		if(dr[i] != 0)
			fout << i << ' ' << dr[i] << '\n';
	
	return 0;
}