Cod sursa(job #240864)

Utilizator dragosmihaiDragos Oana dragosmihai Data 8 ianuarie 2009 20:30:18
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 2.99 kb
#include <stdio.h>
#include <queue>

using namespace std;

struct nod
{
    nod *urm, *coresp;
    int nd,ct,rz;
} *li[20005],*t[20005];

int viz[20001],flx,n,m,s,d;

void adauga(int x,int y)
{
    nod *q=new nod;
    nod *t=new nod;
    q->ct=1;
    t->ct=0;
    q->nd=y;
    t->nd=x;
    q->coresp=t;
    t->coresp=q;
    q->urm=li[x];
    li[x]=q;
    t->urm=li[y];
    li[y]=t;
}

void citire()
{
    freopen("cuplaj.in","r",stdin);
    int e;
    //f>>n>>m>>e;
    scanf("%d %d %d",&n,&m,&e);
    s=0;
    d=n+m+1;
    int a,b;
    for(int i=0;i<e;i++)
    {
        scanf("%d %d",&a,&b);
        adauga(a,b+n);
    }
    for(int i=1;i<=n;i++)
        adauga(s,i);
    for(int i=n+1;i<=n+m;i++)
        adauga(i,d);
}

int flux()
{
    memset(viz,0,sizeof(viz));
    t[s]=0;
    viz[s]=1;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int vf=q.front();
        q.pop();
        for(nod *v=li[vf];v;v=v->urm)
            if(!viz[v->nd] && v->ct >0)
            {
                viz[v->nd]=1;
                q.push(v->nd);
                t[v->nd]=v;
                if(v->nd == d)
                {
                    for(nod *v2=t[d];v2;v2=t[v2->coresp->nd])
                    {
                        v2->ct--;
                        v2->coresp->ct++;
                    }
                    return 1;
                }
            }
    }
    return 0;

    /*
    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=1;i<=n;i++)
        for(nod *v=li[i];v;v=v->urm)
            if(v->coresp->ct==1 && v->nd>n)
            {

                //g<<i<<" "<<v->nd - n <<endl;
                printf("%d %d\n",i,v->nd - n);
                break;
            }
}


int main()
{
    freopen("cuplaj.out","w",stdout);
    citire();
    while (flux())
        flx++;
    printf("%d\n",flx);
    cuplaje();
    return 0;
}