Cod sursa(job #1761009)

Utilizator tudi98Cozma Tudor tudi98 Data 21 septembrie 2016 18:02:03
Problema Cuplaj maxim in graf bipartit Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define ROF(i,a,b) for(int i = a; i >= b; i--)
#define Nmax 10005
#define pb push_back

int n,m,e;
int L[Nmax+1],R[Nmax+1];
vector<int> G[Nmax+1];
bool seen[Nmax+1];

bool cuplat(int x)
{
    if (seen[x])
        return 0;
    seen[x] = 1; 
    
    FOR(i,0,G[x].size()-1) {
        if (R[G[x][i]] == 0 || cuplat(R[G[x][i]])) {
            L[x] = G[x][i];
            R[G[x][i]] = x;
            return 1;
        }
    }

    return 0;
}

int main()
{
    ifstream fin("cuplaj.in");
    ofstream fout("cuplaj.out");

    fin >> n >> m >> e;
    FOR(i,1,e) {
        int x,y;
        fin >> x >> y;
        G[x].pb(y);
    }

    int sol = 0;
    bool changed = 1;
    while (changed) {
        fill_n(seen+1,n,0);
        changed = 0;
        FOR(i,1,n) {
            if (L[i] == 0 && cuplat(i)) {
                sol++;
                changed = 1;
            }
        }
    }

    fout << sol << "\n";
    FOR(i,1,n)
        if (L[i])
            fout << i << " " << L[i] << "\n";

}