Cod sursa(job #2651548)

Utilizator razviii237Uzum Razvan razviii237 Data 22 septembrie 2020 22:01:13
Problema Cuplaj maxim in graf bipartit Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>

using namespace std;

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

const int maxn = 10005;

vector <int> nod[maxn];

int a, b, m, x, y, ans;
int l[maxn], r[maxn];
bool viz[maxn];

bool check(int x) {
    if(viz[x] == true) {
        return false;
    }
    viz[x] = true;
    for(auto u : nod[x]) {
        if(l[u] == false) {
            l[u] = x;
            r[x] = u;
            return true;
        }
    }
    for(auto u : nod[a]) {
        if(check(l[u]) == true) {
            r[x] = u;
            l[u] = x;
            return true;
        }
    }
    return false;
}

int main()
{
    int i;
    f >> a >> b >> m;
    for(i = 1; i <= m; i ++) {
        f >> x >> y;
        nod[x].push_back(y);
    }
    bool ok;
    do {
        ok = false;
        memset(viz, false, sizeof(viz));
        for(i = 1; i <= a; i ++) {
            if(r[i] == false && check(i) == true) {
                ans ++;
                ok = true;
            }
        }
    } while(ok == true);

    g << ans << '\n';
    for(i = 1; i <= a; i ++) {
        if(r[i] != 0) {
            g << i << ' ' << r[i] << '\n';
        }
    }

    f.close();
    g.close();
    return 0;
}