Cod sursa(job #1444395)

Utilizator diana-t95FMI Tudoreanu Diana Elena diana-t95 Data 29 mai 2015 18:34:01
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 10100
int n, m, p;
int l[maxn], r[maxn], viz[maxn];
vector<int> e[maxn];
int match (int x)
{
    if (viz[x]) return 0;
    viz[x] = 1;
    for (int right: e[x])
    {
        if(!r[right]) {
            r[right] = x;
            l[x] = right;
            return 1;
        }

    }
    for (int right: e[x])
    {
        if(match(r[right]))  {
            r[right] = x;
            l[x] = right;
            return 1;
        }
    }
    return 0;
}
int main()
{
    ifstream fin("cuplaj.in");
    ofstream fout("cuplaj.out");
    fin>>n>>m>>p;
    int x, y;
    for (int i = 1; i <= p; i++)
    {
        fin>>x>>y;
        e[x].push_back(y);
    }
    int change = 1;
    while (change)
    {
        change = 0;
        for (int i = 1; i <= n; i++) viz[i] = 0;
        for (int i = 1; i <= n ;i++)
            if (l[i] == 0)
                change = change | match(i);
    }
    int answer = 0;
    for (int i = 1; i <= n; i++)
        if (l[i]) answer++;
    fout<<answer<<'\n';
    for (int i = 1; i <= n; i++)
        if (l[i]) fout<<i<<' '<<l[i]<<'\n';
}