Cod sursa(job #240771)

Utilizator dragosmihaiDragos Oana dragosmihai Data 8 ianuarie 2009 17:56:44
Problema Cuplaj maxim in graf bipartit Scor 4
Compilator cpp Status done
Runda Arhiva educationala Marime 1.89 kb
#include <fstream>
#include <iostream>
#include <queue>

using namespace std;

int c[500][500],viz[20001],flx,t[20001],n,m,s,d;
ofstream g("cuplaj.out");

void citire()
{
    ifstream f("cuplaj.in");
    int e;
    f>>n>>m>>e;
    s=0;
    d=n+m+1;
    for(int i=0;i<e;i++)
    {
        int a,b;
        f>>a>>b;
        c[a][b+n]=1;
        c[b+n][a]=1;
        c[s][a]=1;
        c[a][s]=1;
        c[b+n][d]=1;
        c[d][b+n]=1;
    }
}

void flux()
{
    queue<int> q;
    q.push(s);
    t[s]=-1;
    viz[s]=1;
    while(1)
    {
    int ok=0;
    while(!q.empty())
    {
        int vf=q.front();
        q.pop();
        for(int i = 0;i <= n+m+1;i ++)
            if(c[vf][i] != 0 && viz[i] == 0)
            {
                viz[i] = 1;
                t[i] = vf;
                q.push(i);
                if(i == d)
                {
                    ok=1;
                    break;
                }
            }
        if(ok)
            break;
    }
        if(ok == 0)
            return;
        while(!q.empty())
            q.pop();
        memset(viz,0,sizeof(viz));
        int drum=65000;
        for(int i = d; t[i] != -1; i = t[i])
            if(c[t[i]][i] < drum)
                drum = c[t[i]][i];
        if(drum == 65000)
            return;
        viz[s] = 1;

        q.push(s);
        for(int i = d; t[i] != -1; i = t[i])
        {
            c[t[i]][i] -= drum;
            c[i][t[i]] += drum;
        }
        memset(t,0,sizeof(t));
        t[s] = -1;
        flx += drum;
    }
}

void cuplaje()
{
    for(int i=n+1;i<=n+m;i++)
        for(int j=1;j<=n;j++)
            if(c[i][j]==2)
                {
                    g<<j<<" "<<i-n<<endl;
                    break;
                }
}


int main()
{
    citire();
    flux();
    g<<flx<<endl;
    cuplaje();
    return 0;
}