Cod sursa(job #1397749)

Utilizator stefanzzzStefan Popa stefanzzz Data 23 martie 2015 18:51:39
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <stdio.h>
#include <vector>
#include <string.h>
#define MAXN 10005
using namespace std;

int n, m, edges, l[MAXN], r[MAXN], x, y, cupl;
vector<int> GL[MAXN];
bool change, uz[MAXN];

bool pairup(int p){
	if(uz[p]) return 0;
	uz[p] = 1;

	for(auto x: GL[p])
		if(!r[x]){
			l[p] = x;
			r[x] = p;
			return 1;
		}
	
	for(auto x: GL[p])
		if(pairup(r[x])){
			l[p] = x;
			r[x] = p;
			return 1;
		}
	
	return 0;
}



int main(){
	freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	int i;

	scanf("%d %d %d", &n, &m, &edges);
	while(edges--){
		scanf("%d %d", &x, &y);
		GL[x].push_back(y);
	}

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

	for(i = 1; i <= n; i++)
		cupl += (l[i] != 0);
	
	printf("%d\n", cupl);
	for(i = 1; i <= n; i++)
		if(l[i])
			printf("%d %d\n", i, l[i]);

	return 0;
}