Cod sursa(job #1522382)

Utilizator sebinechitasebi nechita sebinechita Data 11 noiembrie 2015 17:17:39
Problema Cuplaj maxim in graf bipartit Scor 24
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define MAX 10010
#define cout fout
typedef vector<int> :: iterator iter;
vector<int> s[MAX], d[MAX];
int st[MAX], dr[MAX], viz[MAX], n, m, e, x, y, ok;

bool cuplaj(int nod)
{
    if(viz[nod])
        return 0;
    viz[nod] = 1;
    for(iter it = s[nod].begin() ; it != s[nod].end() ; it++)
    {
        if(!st[*it])
        {
            dr[nod] = *it;
            st[*it] = nod;
            return 1;
        }
    }
    for(iter it = s[nod].begin() ; it != s[nod].end() ; it++)
    {
        if(cuplaj(dr[*it]))
        {
            dr[nod] = *it;
            st[*it] = nod;
            return 1;
        }
    }
    return 0;
}

int main()
{
    int i;
    fin >> n >> m >> e;
    while(e--)
    {
        fin >> x >> y;
        s[x].push_back(y);
        d[y].push_back(x);
    }
    ok = 1;
    int sum = 0;
    while(ok)
    {
        ok = 0;
        for(i = 1 ; i <= n ; i++)
        {
            if(!dr[i] && cuplaj(i))
            {
                sum++;
                ok = 1;
            }
        }
        for(i = 1 ; i <= n ; i++)
        {
            viz[i] = 0;
        }
    }
    cout << sum << "\n";
    for(i = 1 ; i <= n ; i++)
    {
        if(dr[i])
        cout << i << " " << dr[i] << "\n";
    }
}