Cod sursa(job #1699696)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 8 mai 2016 11:55:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.58 kb
// Template v2
#define pb push_back
#define mp make_pair
#define det(a,b,c,d)  (a*d-b*c)
#define first x
#define second y
#define lsb(x) x & -x
#define PI   3.14159265358979323846
#include<fstream>
#include<vector>
#include<iomanip>
#include<unordered_map>
#include<algorithm>
#include<string.h>
#include<stack>
#include<set>
#include <bitset>
using namespace std;
  
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PKK;
// primes less than 100
const int PRIM[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; const int CMAX = 100069;
const int MOD1 = 10039;
const int MOD2 = 10067;
const int MOD3 = 10429;
const int P = 73;
const int NMAX = 20005;
const double EPS = 1e-7;
const int INF16 = 320000;
const int INF = 2*1e9 + 6661369;
const LL INF64 = LL(1e18);
const int dx[]={-1,1,0,0};
const int dy[]={0,0,1,-1};

ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");

int l[NMAX], r[NMAX];
VI G[NMAX];
bool viz[NMAX];
int n,m,e,x,y,rs;

bool dfs(int v)
{
	if(viz[v])
		return 0;
	viz[v]=1;
	for(auto w:G[v])
		if(l[w] == 0 || dfs(l[w]))
		{
			l[w]=v;
			r[v]=w;
			return 1;
		}
	return 0;
}

void read()
{
	cin>>n>>m>>e;
	for(int i=1; i<=e; ++i)
	{
		cin>>x>>y;
		G[x].pb(y+n);
	}
	bool q=1;
	while(q)
	{
		q=0;
		memset(viz, 0, sizeof(viz));
		for(int i=1; i<=n; ++i)
		{
			if(r[i]==0 && dfs(i))
			{
				q=1;
				rs++;
			}
		}
	}
	cout<<rs<<"\n";
	for(int i=1; i<=n; ++i)
		if(r[i])
			cout<<i<<" "<<r[i]-n<<"\n";
}

int main()
{
	read();
	return 0;
}