Mai intai trebuie sa te autentifici.

Cod sursa(job #962238)

Utilizator timicsIoana Tamas timics Data 14 iunie 2013 09:32:52
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include<stdio.h>
#include<vector>
using namespace std;
int L,R,E,x,y,PairL[11000],PairR[11000],Dist[22000],Q[22000],first=1,last=0;
vector<int> a[22000];

bool bfs()
{
    for(int i=1;i<=L;++i)
    {
        if(PairL[i]==0)
        {
            Dist[i]=0;
            ++last;
            Q[last]=i;
        }
        else
            Dist[i]=1000000;
    }
    Dist[0]=1000000;

    while(first<=last)
    {
        int y=Q[first];
        ++first;
        if(Dist[y]<Dist[0])
        {
            int z=a[y].size();
            for(int i=0;i<z;++i)
                if(Dist[PairR[a[y][i]]]==1000000)
                {
                    Dist[PairR[a[y][i]]]=Dist[y]+1;
                    ++last;
                    Q[last]=PairR[a[y][i]];
                }
        }
    }
    return Dist[0]!=1000000;
}

bool dfs(int x)
{
    if(x!=0)
    {
        int y=a[x].size();
        for(int i=0;i<y;++i)
        {
            if(Dist[PairR[a[x][i]]]==Dist[x]+1)
                if (dfs(PairR[a[x][i]]))
                {
                    PairR[a[x][i]]=x;
                    PairL[x]=a[x][i];
                    return 1;
                }

            Dist[x]=1000000;
            return 0;
        }
        return 1;

    }
}

int hopcroft()
{
    int ret=0;

    for(int i=1;i<=R+L;++i)
    {
        PairL[i]=0;
        PairR[i]=0;

    }

    while(bfs())
        for(int i=1;i<=L;++i)
            if(PairL[i]==0)
                if(dfs(i))
                    ++ret;

    return ret;
}

int main()
{
    freopen("cuplaj.in","r",stdin);
    //freopen("cuplaj.out","w",stdout);
    scanf("%d%d%d",&L,&R,&E);
    for(int i=1;i<=E;++i)
    {
        scanf("%d%d",&x,&y);
        a[x].push_back(y);
        a[y].push_back(x);
    }
    printf("%d",hopcroft());
    return 0;
}