Cod sursa(job #1549335)

Utilizator cristina_borzaCristina Borza cristina_borza Data 12 decembrie 2015 11:29:49
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <cstring>
#include <vector>

#define NMAX 100005

using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

pair <int , int> sol[NMAX];

vector <vector <int>> v;

int a , b , m , node1 , node2 , c;
int r[NMAX] , viz[NMAX];

int pair_up(int node);

int main() {
    f >> a >> b >> m;

    v.resize(a + 5);

    for(int i = 1 ; i <= m ; ++i) {
        f >> node1 >> node2;
        v[node1].push_back(node2);
    }

    for(int i = 1 ; i <= a ; ++i) {
        memset(viz , 0 , sizeof(viz));
        c += pair_up(i);
    }

    g << c << '\n';

    memset(viz , 0 , sizeof(viz));
    for(int i = 1 ; i <= b ; ++i) {
        if(r[i] != 0) {
            viz[r[i]] = i;
        }
    }

    for(int i = 1 ; i <= a ; ++i) {
        if(viz[i] != 0) {
            g << i << " " << viz[i] << '\n';
        }
    }
    return 0;
}

int pair_up(int node) {
    if(viz[node]) {
        return 0;
    }

    viz[node] = 1;
    for(vector <int> :: iterator it = v[node].begin() ; it != v[node].end() ; ++it) {
        int p = *it;
        if(r[p] == 0 || pair_up(r[p])) {
            sol[c + 1].first = node;
            sol[c + 1].second = *it;
            r[*it] = node;
            return 1;
        }
    }

    return 0;
}