Cod sursa(job #938040)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 11 aprilie 2013 17:40:39
Problema Cuplaj maxim in graf bipartit Scor 16
Compilator cpp Status done
Runda Arhiva educationala Marime 1.98 kb
#include <fstream>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

#define inf 1<<30
#define LE 1066
#include <vector>
#include <map>
#define pb push_back
#define mp make_pair

vector<int> H[LE*2+666];
int n,i,fmin,e,xx,yy,color,node,maxn;
int viz[LE],father[LE],m,result,cap;
map<pair<int,int>,int >flux;
bool okz=true;

void dfs(int nod)
{
    if (okz==true)
        return;
    if (nod==n)
    {
        okz=true;
        return;
    }
    viz[nod]=color;
    int N=H[nod].size();

    for(int i=0; i<N; ++i)
    {
        if (nod>maxn&&H[nod][i]&&H[nod][i]<=maxn)
            cap=0;
        else
            cap=1;

        if(viz[H[nod][i]]!=color&&cap>flux[mp(nod,H[nod][i])])
        {
            father[H[nod][i]]=nod;
            dfs(H[nod][i]);
        }
    }
}

int main()
{
    f>>n>>m>>e;
    maxn=n;

    for(i=1; i<=e; ++i)
    {
        f>>xx>>yy;
        H[xx].pb(yy+n);
        H[yy+n].pb(xx);

        flux[mp(xx,yy)]=0;
        flux[mp(yy,xx)]=0;
    }

    for(i=1; i<=n+m; ++i)
    {
        if (i<=n)
        {
            H[0].pb(i);
            flux[mp(0,i)]=0;
            flux[mp(i,0)]=0;
        }
        else
        {
            H[i].pb(n+m+1);
            flux[mp(i,n+m+1)]=0;
            flux[mp(n+m+1,i)]=0;
        }
    }
    n+=(m+1);

    while (okz)
    {
        okz=false;
        fmin=0;
        ++color;
        dfs(0);
        if (okz==false)
            break;
        node=n;

        while(node)
        {
            flux[mp(father[node],node)]++;
            flux[mp(node,father[node])]--;
            node=father[node];
        }
        ++result;
    }

    g<<result<<'\n';

    for(i=1; i<=maxn; ++i)
    {
        int N=H[i].size();

        for(int j=0; j<N; ++j)
            if (i<H[i][j])
                if (flux[mp(i,H[i][j])]>0)
                    g<<i<<" "<<H[i][j]<<'\n';
    }


    f.close();
    g.close();
    return 0;
}