Cod sursa(job #1517892)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 4 noiembrie 2015 23:13:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int nmax = 10000;

vector <int> g[nmax+5];
bool viz[nmax+5];
int n, m, l[nmax+5], r[nmax+5];

int cup(int x)
{
    if(viz[x])return 0;
    viz[x] = true;
    vector <int> :: iterator it = g[x].begin();
    while(it!=g[x].end())
    {
        if(!r[*it])
        {
            l[x] = *it;
            r[*it] = x;
            return 1;
        }
        it++;
    }
    it = g[x].begin();
    while(it!=g[x].end())
    {
        if(cup(r[*it]))
        {
            l[x] = *it;
            r[*it] = x;
            return 1;
        }
        it++;
    }
    return 0;
}

int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    int e;
    scanf("%d%d%d", &n, &m, &e);
    for(int i=0; i<e; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        g[x].push_back(y);
    }
    bool ok = true;
    while(ok)
    {
        ok = false;
        memset(viz, 0, sizeof(viz));
        for(int i=1; i<=n; i++)
            if(!l[i] && cup(i))
                ok = true;
    }
    int cuplaj = 0;
    for(int i=1; i<=n; i++)
        if(l[i])cuplaj++;
    printf("%d\n", cuplaj);
    for(int i=1; i<=n; i++)
        if(l[i])printf("%d %d\n", i, l[i]);
    return 0;
}