Cod sursa(job #971639)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 9 iulie 2013 19:34:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

#define newn a[x][i]

using namespace std;

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

const int N = 10010;

int t, n, m, e, sol, st[N], dr[N];
vector <int> a[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++)
        if(!dr[newn])
        {
            st[x] = newn; dr[newn] = x;
            return 1;
        }
    for(unsigned i=0; i<a[x].size(); i++)
        if(leaga(dr[newn]))
        {
            st[x] = newn; dr[newn] = x;
            return 1;
        }
    return 0;
}

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

        bool ok = 1;
        while(ok)
        {
            ok = 0;
            for(int i=0; i<=n; i++) viz[i] = 0;
            for(int i=1; i<=n; i++)
                if(!st[i] && leaga(i)) ok = 1, sol++;
        }
        fout<<sol<<'\n';
        for(int i=1; i<=n; i++)
            if(st[i]) fout<<i<<' '<<st[i]<<'\n';
    return 0;
}