Cod sursa(job #402005)

Utilizator raica_cristiraica dumitru cristian raica_cristi Data 23 februarie 2010 12:09:58
Problema Cuplaj maxim in graf bipartit Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include<stdio.h>
#include<algorithm>
#include<vector>

using namespace std;

#define dim 1<<14 
#define pb push_back
#define sz size()
#define max 10100

int dr[max],st[max];
char viz[max];
int n,m;

vector <int> v[dim];
void read()
{
    int x,y,o;
    scanf("%d %d %d",&n,&m,&o);
    for(int i=1;i<=o;i++)
    {
        scanf("%d%d",&x,&y);
        v[x].pb(y);
    }
}
void init_viz()
{
    for(int i=1;i<=n;i++)
        viz [ i ] = 0;
}
int pairup(int nod)
{
    unsigned int i; 
    if( viz[nod] )
    return 0;
    viz [ nod ] = 1;
    for( i=0;  i < v[nod].sz ; i++)
    {
        if( ! dr [ v[nod][i] ] )
        {
            st[ nod ] = v [nod][i];
            dr[ v[ nod][i] ] = nod;
            return 1;
        }
    }
    
    for( i=0 ; i< v[nod].sz ; i++)
    {
        if(  pairup (dr [ v [nod][i] ] ) )
        {
            dr [ v[nod][i] ] = nod ;
            st [ nod ] = v[nod][i] ;
            return 1; 
        }
        
    }
    return 0;
}
void solve()
{
    int ok=0,cuplaj=0;
    for(ok=1 ; ok ; )
    {
        ok=0;
        init_viz();
    for(int i=1;i<=n;i++)
    if( ! st[ i ] &&  pairup(i) )
    {
        cuplaj ++;
        ok = 1;
    }  
}
    printf("%d\n",cuplaj);
  for(int i=1;i<=cuplaj;i++)
    printf("%d %d\n",i,dr[i]);}
int main ()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    read();
    solve();
}