Cod sursa(job #966421)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 25 iunie 2013 21:21:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

#define N 10005

int n, m, p, sol;
vector <int> a[N], s(N), dr(N);
vector <bool> viz(N);

bool leaga(int x)
{
    if(viz[x]) return 0;
    viz[x] = 1;
    for(unsigned i=0; i<a[x].size(); i++)
    {
        int y = a[x][i];
        if(!dr[y] || leaga(dr[y]))
        {
            s[x] = y; dr[y] = x;
            return 1;
        }
    }
    return 0;
}

int main()
{
    fin>>n>>m>>p;
    while(p--)
    {
        int x, y;
        fin>>x>>y;
        a[x].push_back(y);
    }

    bool ok = 1;
    while(ok)
    {
        ok = 0;
        for(int i=1; i<=n; i++) viz[i] = 0;
        for(int i=1; i<=n; i++)
            if(!s[i])
                if(leaga(i))
                 sol++, ok = 1;
    }

    fout<<sol<<'\n';
    for(int i=1; i<=n; i++)
        if(s[i]) fout<<i<<' '<<s[i]<<'\n';
    return 0;
}