Cod sursa(job #2101899)

Utilizator LucaSeriSeritan Luca LucaSeri Data 8 ianuarie 2018 11:00:25
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e4 + 10;

vector < int > gr[MAXN];
int boss_left[MAXN];
int boss_right[MAXN];

bool viz[MAXN];

bool cuplaj(int node){
    if(viz[node]) return false;
    viz[node] = true;
    for(auto x : gr[node]){
        if(boss_left[x] == 0){
            boss_left[x] = node;
            boss_right[node] = x;
            return true;
        }
    }

    for(auto x : gr[node]){
        if(cuplaj(boss_left[x])){
            boss_left[x] = node;
            boss_right[node] = x;
            return true;
        }
    }

    return false;
}
int main(){
    ifstream f("cuplaj.in");
    ofstream g("cuplaj.out");

    int n, m, e;
    f >> n >> m >> e;
    for(int i = 1; i <= e; ++i){
        int a, b;
        f >> a >> b;
        gr[a].push_back(b);
    }

    bool ok = true;
    while(ok){
        ok = false;
        memset(viz, 0, sizeof viz);
        for(int i = 1; i <= n; ++i){
            if(boss_right[i] == 0)
                ok |= cuplaj(i);
        }
    }

    int cnt = 0;
    for(int i = 1; i <= n; ++i){
        if(boss_right[i]) ++ cnt;
    }

    g << cnt << '\n';
    for(int i = 1; i <= n; ++i){
        if(boss_right[i]) g << i << ' ' << boss_right[i] << '\n';
    }
    return 0;
}