Cod sursa(job #2437618)

Utilizator caesar2001Stoica Alexandru caesar2001 Data 9 iulie 2019 21:08:20
Problema Cuplaj maxim in graf bipartit Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

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

int go(int node, vector<bool> &vis, const vector< vector<int> > &g, vector<int> &r, vector<int> &l) {
    vis[node] = 1;

    for(auto it : g[node]) {
        if(!l[it] || (!vis[l[it]] && go(l[it], vis, g, r, l))) {
            r[node] = it;
            l[it] = node;
            return 1;
        }
    }

    return 0;
}

int main() {

    int n, m, e;
    in >> n >> m >> e;
    vector< vector<int> > g(n + 1);
    for(int i = 1; i <= e; i ++) {
        int x, y;
        in >> x >> y;
        g[x].push_back(y);
    }

    int ans = 0, ok = 1;
    vector<int> match_to_right(n + 1, 0);
    vector<int> match_to_left(m + 1, 0);
    while(ok) {
        ok = 0;
        vector<bool> vis(n + 1, 0);

        for(int i = 1; i <= n; i ++)
            if(!vis[i] && !match_to_left[i])
                ok += go(i, vis, g, match_to_right, match_to_left);
        ans += ok;
    }

    out << ans << "\n";
    for(int i = 1; i <= n; i ++)
        if(match_to_right[i])
            out << i << " " << match_to_right[i] << "\n";


    return 0;
}