Cod sursa(job #2636033)

Utilizator irimia_alexIrimia Alex irimia_alex Data 16 iulie 2020 12:50:01
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <stdio.h>
#include <cstring>
#include <utility>
#include <vector>
#include <time.h>
#define NMAX 20005

using namespace std;

FILE* fin, * fout;

int n, m, e;
vector<int> g[NMAX];
bool viz[NMAX] = { 0 };
int left_match[NMAX] = { 0 }, right_match[NMAX] = { 0 };

bool dfs(int u) {
	if (viz[u] == true) return false;
	viz[u] = true;
	for (auto v : g[u]) {
		if (left_match[v] == 0 || dfs(left_match[v])) {
			left_match[v] = u;
			right_match[u] = v;
			return true;
		}
	}
	return false;
}

void maximumMatching() {
	int res = 0;
	bool change = true;
	while (change) {
		change = false;
		memset(viz, 0, sizeof(viz));
		for (int i = 1;i <= n;++i)
			if (right_match[i] == 0)
				change |= dfs(i);
	}

	for (int i = n + 1;i <= n + m;++i)
		if (left_match[i]>0) ++res;

	fprintf(fout, "%i\n", res);
	for (int i = n + 1;i <= n + m;++i)
		if (left_match[i] != 0)
			fprintf(fout, "%i %i\n", left_match[i], i - n);
}

int main() {
	clock_t t = clock();

	fin = fopen("cuplaj.in", "r");
	fout = fopen("cuplaj.out", "w");

	fscanf(fin, "%i %i %i", &n, &m, &e);
	while (e--) {
		int x, y;
		fscanf(fin, "%i %i", &x, &y);
		g[x].push_back(y + n);
	}

	maximumMatching();

	printf("%f", (float)(clock() - t) / CLOCKS_PER_SEC);

	return 0;
}