Cod sursa(job #263152)

Utilizator ProtomanAndrei Purice Protoman Data 19 februarie 2009 22:34:20
Problema Cuplaj maxim in graf bipartit Scor 28
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <string.h>

#define pb push_back
#define MAX 10010
#define vctIntIter vector <int>::iterator

using namespace std;

int nrSour, nrDest, nrEdges, sol;
int vctDr[MAX], vctSt[MAX], sel[MAX];
vector <int> listDrum[MAX];

inline int cupleaza(int nod)
{
    if (sel[nod])
        return 0;

    sel[nod] = 1;

    for (vctIntIter it = listDrum[nod].begin(); it != listDrum[nod].end(); it++)
        if (!vctSt[*it])
        {
            vctSt[*it] = nod;
            vctDr[nod] = *it;

            return 1;
        }

    for (vctIntIter it = listDrum[nod].begin(); it != listDrum[nod].end(); it++)
        if (cupleaza(*it))
        {
            vctSt[*it] = nod;
            vctDr[nod] = *it;

            return 1;
        }

    return 0;
}


int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);

    scanf("%d %d %d", &nrSour, &nrDest, &nrEdges);

    for (; nrEdges; nrEdges--)
    {
        int t, f;
        scanf("%d %d", &t, &f);

        listDrum[t].pb(f);
    }

    for (int i = 1; i <= nrSour; i++)
    {
        if (vctDr[i])
            continue;

        memset(sel, 0, sizeof(sel));
        sol += cupleaza(i);
    }

    printf("%d\n", sol);

    for (int i = 1; i <= nrSour; i++)
        if (vctDr[i])
            printf("%d %d\n", i, vctDr[i]);

    fclose(stdin);
    fclose(stdout);
    return 0;
}