Cod sursa(job #1907480)

Utilizator andrew_assassin789Andrei Manea andrew_assassin789 Data 6 martie 2017 19:28:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <fstream>
#include <vector>
#include <cstring>
#define nmax 10005
using namespace std;
vector <unsigned int> a[nmax];
unsigned int l[nmax],r[nmax];
bool viz[nmax];
bool cuplare(unsigned int x)
{
    if (viz[x]) return 0;
    viz[x]=1;
    unsigned int i,y;
    for (i=0;i<a[x].size();i++)
    {
        y=a[x][i];
        if ((!r[y])||cuplare(r[y]))
        {
            l[x]=y;
            r[y]=x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    ifstream f("cuplaj.in");
    ofstream g("cuplaj.out");
    unsigned int i,x,n,m,e,y,cuplaj=0,ok=1;
    f>>n>>m>>e;
    for (i=1;i<=e;i++)
    {
        f>>x>>y;
        a[x].push_back(y);
    }
    while (ok)
    {
        ok=0;
        memset(viz,0,sizeof(viz));
        for (i=1;i<=n;i++)
        {
            //fiecare nod din L
            if (!l[i])
            {
                ok= ok | (cuplare(i));
            }
        }
    }
    for (i=1,cuplaj=1;i<=n;i++,cuplaj+=(l[i]>0));
    g<<cuplaj<<'\n';
    for (i=1;i<=n;i++)
    {
        if (l[i]) g<<i<<' '<<l[i]<<'\n';
    }
    f.close();
    g.close();
    return 0;
}