Cod sursa(job #2136960)

Utilizator bcrisBianca Cristina bcris Data 20 februarie 2018 14:44:23
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <vector>
#include <iostream>
#include <queue>

using namespace std;
#define MAX_SIZE 100000
#define INF 1000000

vector<int> graph[MAX_SIZE]; 
int n, m, e;
int l[MAX_SIZE], r[MAX_SIZE], used[MAX_SIZE];


int pairup(int node) {
	if (used[node])
		return 0;
	used[node] = 1;
	for (vector<int>::iterator i = graph[node].begin(); i != graph[node].end(); i++) {
		if (!r[*i]) {
			l[node] = *i;
			r[*i] = node;
			return 1;
		}
	}

	for (vector<int>::iterator i = graph[node].begin(); i != graph[node].end(); i++) {
		if (pairup(r[*i])) {
			l[node] = *i;
			r[*i] = node;
			return 1;
		}
	}

	return 0;
}

int main() {
	// read input
	// n - number of vertices in Left
	// m - number of vertices in Right
	// e - number of edges
	freopen("hk.in", "r", stdin);
	freopen("hk.out", "w", stdout);

	scanf("%d %d %d\n", &n, &m, &e);
	int a, b;
	for (int i = 0; i < e; i++) {
		scanf("%d %d\n", &a, &b);
		graph[a].push_back(b);
	}

	int change = 1;
	while (change == 1) {
		change = 0;
		memset(used, 0, sizeof(used));
		for (int i = 1; i <= n; i++) {
			if (!l[i]) {
				change = change | pairup(i);
			}
		}
	}

	int cuplaj = 0;
	for (int i = 1; i <= n; i++) {
		cuplaj += (l[i] > 0);
	}
	printf("%d\n", cuplaj);
	for (int i = 1; i <= n; i++) {
		if (l[i] > 0) {
			printf("%d %d\n", i, l[i]);
		}
	}	

	return 0;
}