Cod sursa(job #1161554)

Utilizator supermitelArdelean Razvan Mitel supermitel Data 31 martie 2014 12:10:50
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <vector>

using namespace std;

bitset<10010> viz;
vector<int> vec[10010];

int R[10010], L[10010];
int n, nl, nrm;

void citire()
{
    int x, y, m;
    scanf("%d%d%d", &n, &nl, &m);
    for(int i = 0;  i < m; i++)
    {
        scanf("%d%d", &x, &y);
        vec[x].push_back(y);
    }
}

bool cuplaj(int x)
{
    if(viz[x])
        return false;
    viz[x] = 1;
    for(int i = 0; i < vec[x].size(); i++)
        if(!L[vec[x][i]] || cuplaj(vec[x][i]))
        {
            R[x] = vec[x][i];
            L[vec[x][i]] = x;
            return true;
        }
    return false;
}

void solve()
{
    int ok = 1;
    while(ok)
    {
        ok = 0;
        viz = 0;
        for(int i = 1; i <= n; i++)
            if(!R[i] && cuplaj(i))
        {
            nrm++;
            ok = 1;
        }
    }
}

void afisare()
{
    printf("%d\n", nrm);
    for(int i = 1; i <= n; i++)
        if(R[i])
            printf("%d %d\n", i, R[i]);
}

int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    citire();
    solve();
    afisare();
    return 0;
}