Cod sursa(job #1786006)

Utilizator BlackNestaAndrei Manaila BlackNesta Data 22 octombrie 2016 11:12:11
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>
#define Nmax 10005

using namespace std;

int st[Nmax], dr[Nmax], ans, n, m;
bool viz[Nmax];
vector <int> L[100005];

void Citire()
{
    ifstream f("cuplaj.in");
    int i, e, x, y;
    f >> n >> m >> e;
    for(i = 1; i <= e; i++)
    {
        f >> x >> y;
        L[x].push_back(y);
    }
    f.close();
}

inline bool Cupleaza(int nod)
{
    if(viz[nod]) return false;
    viz[nod] = true;
    int i, nnod;
    for(i = 0; i < L[nod].size(); i++)
    {
        nnod = L[nod][i];
        if(!dr[nnod])
        {
            st[nod] = nnod;
            dr[nnod] = nod;
            return true;
        }
    }
    for(i = 0; i < L[nod].size(); i++)
    {
        nnod = L[nod][i];
        if(Cupleaza(dr[nnod]))
        {
            st[nod] = nnod;
            dr[nnod] = nod;
            return true;
        }
    }
    return false;
}

void Rezolv()
{
    int i, gata;
    gata = 0;
    while(!gata)
    {
        gata = 1;
        for(i = 1; i <= n; i++)
            viz[i] = 0;
        for(i = 1; i <= n; i++)
            if(!st[i] && Cupleaza(i))
            {
                ans++;
                gata = 0;
            }
    }
    ofstream g("cuplaj.out");
    g << ans << "\n";
    for(i = 1; i <= n; i++)
        if(st[i])
            g << i << " " << st[i] << "\n";
    g.close();
}

int main()
{
    Citire();
    Rezolv();
    return 0;
}