Cod sursa(job #313138)

Utilizator SleepyOverlordPatcas Csaba SleepyOverlord Data 8 mai 2009 00:23:13
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 2.19 kb
//Code by Patcas Csaba
//Time complexity:
//Space complexity:
//Method: Test citire
//Working time: 23:55 - 

#include <stdio.h>
#include <ctype.h>
#include <vector>
#include <string> 
#include <set> 
#include <map> 
#include <iostream> 
#include <sstream> 
#include <numeric> 
#include <algorithm> 
#include <cmath> 
#include <queue> 
#include <bitset> 

using namespace std;

#define in_file "cuplaj.in"
#define out_file "cuplaj.out"

#define VI vector <int>
#define FORN(i,n) for(int i=0;i<n;++i)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define S size()
#define B begin() 
#define E end() 
#define RS resize
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define ALL(x) x.begin(),x.end()
#define repeat do{ 
#define until(x) }while(!(x)); 

#define maxBuf 65536

int n1, n2, m, n, ind, solution;
vector <VI> g;
char buf[maxBuf];
FILE* fin=fopen(in_file,"r");
VI match;
vector <bool> was;

int PairUp(int node)
{
	was[node]=true;
	FORN(i,g[node].S)
	{
		int aux=g[node][i];
		if (!match[aux]) 
		{
			match[aux]=node;
			match[node]=aux;
			return 1;
		}
	}
	FORN(i,g[node].S)
	{
		int aux=g[node][i];
		if (aux==match[node]) continue;
		if (!was[match[aux]] && PairUp(match[aux]))
		{
			match[aux]=node;
			match[node]=aux;
			return 1;
		}
	}
	return 0;
}

void solve()
{
	match.RS(n+1);
	was.RS(n1+1);
	solution=0;
	FOR(i,1,n1)
		if (!match[i])
		{
			fill(ALL(was),false);
			solution+=PairUp(i);
		}
}

int ReadNr()
{
	int ans=0;
	while (ind<maxBuf && isdigit(buf[ind]))
	{
		ans=ans*10+buf[ind]-'0';
		++ind;
	}
	if (ind==maxBuf)
	{
		fread(buf,1,maxBuf,fin);
		ind=0;
		while (ind<maxBuf && isdigit(buf[ind]))
		{
			ans=ans*10+buf[ind]-'0';
			++ind;
		}
	}
	++ind;
	return ans;
}

int main()
{
	fscanf(fin,"%d%d%d\n",&n1,&n2,&m);
	n=n1+n2;
	g.RS(n+1);
	fread(buf,1,maxBuf,fin);
	ind=0;
	FORN(i,m)
	{
		int x,y;
		x=ReadNr();
		y=ReadNr();
		g[x].PB(n1+y);
		g[n1+y].PB(x);
	}
	fclose(fin);

	solve();

	FILE* fout=fopen(out_file,"w");
	fprintf(fout,"%d\n",solution);
	FOR(i,1,n1)
		if (match[i]) fprintf(fout,"%d %d\n",i,match[i]-n1);
	fclose(fout);
	
	return 0;
}