Cod sursa(job #758398)

Utilizator SzakatsSzakats Istvan Szakats Data 15 iunie 2012 16:20:34
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.56 kb
#include <stdio.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <deque>
#include <string.h>

using namespace std;

typedef pair<int, int> PII;

#ifdef _WIN32
#define TYPEOF decltype
#else
#define TYPEOF typeof
#endif

#define FOR(i,s,e) for(int i = s;i < e; i++)
#define TR(i, c) for(TYPEOF(c.begin()) i = c.begin(); i != c.end(); ++i)
#define TRS(i, c, ...) TR(__itr, c) { TYPEOF(*c.begin()) &i = *__itr;  __VA_ARGS__ }
#define TRP(f, s, c, ...) TR(__itr, c) { TYPEOF(c.begin()->first) &f = __itr->first; TYPEOF(c.begin()->second) &s = __itr->second;  __VA_ARGS__ }

#define MAX_NM 10007

int N, M, E;

int l[MAX_NM], r[MAX_NM], v[MAX_NM];

vector<int> G[MAX_NM];

int pairup(int x)
{
	if(v[x]) return 0;
	v[x] = 1;
	TRS(y,G[x], if(!r[y]) {
		r[y] = x;
		l[x] = y;
		return 1;
	})
	TRS(y,G[x], if(pairup(r[y])) {
		r[y] = x;
		l[x] = y;
		return 1;
	})
	return 0;
}

int main()
{
#if 1
	freopen("cuplaj.in", "r", stdin);
#ifndef MY_STUFF
	freopen("cuplaj.out", "w", stdout);
#endif
#endif

	int t = 1;
	//scanf("%d", &t);
	while(t--)
	{
		scanf("%d %d %d", &N, &M, &E);
		FOR(i,0,E)
		{
			int x,y;
			scanf("%d %d", &x, &y);
			G[x].push_back(y);
		}
		
		for (int change = 1; change; ) {
			change = 0;
			memset(v, 0, sizeof(v));
			FOR(i, 1, N+1) if(!l[i])
				change |= pairup(i);
		}
		int nr = 0;
		FOR(i,1,M+1) if(r[i]) nr++;
		printf("%d\n", nr);
		FOR(i,1,N+1) if(l[i]) printf("%d %d\n", i, l[i]);
	}

	return 0;
}