Cod sursa(job #757911)

Utilizator the@EyE@Postavaru Stefan the@EyE@ Data 13 iunie 2012 19:08:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<vector>
#include<cstring>
#include<stdio.h>
#define INF 10001

using namespace std;

vector<int> graph[INF];
int n,m,e,used[INF],match1[INF],match2[INF];


int match(int ind)
{
    if(used[ind])return 0;
    used[ind]=1;

    for(vector<int>::iterator it=graph[ind].begin();it!=graph[ind].end();it++)
        if(match2[*it]==0)
        {
            match1[ind]=*it;
            match2[*it]=ind;
            return 1;
        }

    for(vector<int>::iterator it=graph[ind].begin();it!=graph[ind].end();it++)
        if(match(match2[*it]))
        {
            match1[ind]=*it;
            match2[*it]=ind;
            return 1;
        }

    return 0;
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);

    scanf("%d %d %d\n",&n,&m,&e);
    for(int i=0;i<e;++i)
    {
        int x,y;
        scanf("%d %d\n",&x,&y);
        graph[x].push_back(y);
    }

    int wasChanged=1;
    while(wasChanged)
    {
        wasChanged=0;
        memset(used,0,sizeof(used));

        for(int i=1;i<=n;++i)
            if(!match1[i]) wasChanged+=match(i);
    }

    int matches=0;
    for(int i=1;i<=n;i++)
        if(match1[i])matches++;

    printf("%d\n",matches);

    for(int i=1;i<=n;i++)
            if(match1[i])printf("%d %d\n",i,match1[i]);
}