Cod sursa(job #1241289)

Utilizator danalex97Dan H Alexandru danalex97 Data 13 octombrie 2014 10:05:18
Problema Cuplaj maxim in graf bipartit Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

ifstream F("cuplaj.in");
ofstream G("cuplaj.out");

const int N = 10010;

int n,m,e,l[N],r[N],mk[N];
vector<int> v[N];

bool good(int x)
{
    mk[x] = 1;
    for (int i=0;i<v[x].size();++i)
    {
        int y = v[x][i];
        if ( r[y] == 0  )
        {
            l[x] = y;
            r[y] = x;
            return 1;
        }
        else
        {
            if ( mk[r[y]] == 0 )
                if ( good(r[y]) )
                {
                    l[x] = y;
                    r[y] = x;
                    return 1;
                }
        }
    }
    return 0;
}

int main()
{
    F>>n>>m>>e;
    for (int i=1,x,y;i<=e;++i)
    {
        F>>x>>y;
        v[x].push_back(y);
    }
    bool ok = 1;
    int ans = 0;
    while ( ok )
    {
        ok = 0;
        memset(mk,0,sizeof(mk));
        for (int i=1;i<=n;++i)
            if ( r[i] == 0 )
                if ( good(i) )
                {
                    ok = 1;
                    ++ans;
                }
    }
    G<<ans<<'\n';
    for (int i=1;i<=n;++i)
        if ( l[i] )
            G<<i<<' '<<l[i]<<'\n';
}