Cod sursa(job #854060)

Utilizator andrettiAndretti Naiden andretti Data 12 ianuarie 2013 18:41:23
Problema Cuplaj maxim in graf bipartit Scor 16
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include<fstream>
using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int maxn=10010,inf=999999999;
int n1,n2,n,m;
int p,u,nrd=1,flux;
int v[maxn],t[maxn],cc[maxn];
int f[maxn][maxn];

void cit()
{
	int i,x,y,cost;
	
	fin>>n1>>n2>>m;
	
	for(i=1;i<=m;i++)
	{
		fin>>x>>y;
		f[x+1][y+n1+1]=1;
		f[1][x+1]=1;
		f[y+n1+1][n1+n2+2]=1;
	}
	n=n1+n2+2;
}

int drum()
{
    int i,nod;
	
    p=u=1; cc[1]=1; v[1]=nrd; t[1]=0;
	
    while(p<=u)
    {
        nod=cc[p];
		if(nod==n) return 1;
		
        for(i=1;i<=n;i++)
            if(f[nod][i]>0 && v[i]!=nrd)
            {
                u++;
                cc[u]=i;
                t[i]=nod;
                v[i]=nrd;
            }
        p++;
    }
	
    return 0;
}
 
void flux_max()
{
    int i,k;
    
    while(drum())
	{
		for(i=n;t[i]!=0;i=t[i])
		{
			f[t[i]][i]--;
			f[i][t[i]]++;
		}
		flux++;
		nrd++;
	}
    
}

void afis()
{
	int i,st=n1+2,dr=n1+n2+1;
	
	for(i=st;i<=dr;i++)
		fout<<t[i]-1<<" "<<i-n1-1<<'\n';
}		

int main()
{
	cit();
	flux_max();
	fout<<flux<<'\n';
	afis();
	
	fin.close();
	fout.close();
	return 0;
}