Cod sursa(job #2794157)

Utilizator vladth11Vlad Haivas vladth11 Data 4 noiembrie 2021 13:23:50
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.73 kb
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <ll, pii> muchie;

const ll NMAX = 20001;
const ll INF = (1LL << 60) - 1;
const ll MOD = 1000000007;
const ll BLOCK = 101;

int match[NMAX];
int viz[NMAX];
vector <int> v[NMAX];

bool DFS(int node) {
    viz[node] = 1;
    for(auto x : v[node]) {
        int nxt = match[x];
        if(nxt != 0) {
            if(!viz[nxt] && DFS(nxt)){
                match[x] = node;
                match[node] = x;
                return 1;
            }
        }else{
            match[x] = node;
            match[node] = x;
            return 1;
        }
    }
    return 0;
}

int main() {
    ifstream cin("cuplaj.in");
    ofstream cout("cuplaj.out");
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m, e, i;
    cin >> n >> m >> e;
    for(i = 1; i <= e; i++) {
        int x, y;
        cin >> x >> y;
        v[x].push_back(y + n);
        v[y + n].push_back(x);
    }
    n += m;
    int ok = 1, cuplaj = 0;
    while(ok) {
        ok = 0;
        for(i = 1; i <= n; i++) {
            viz[i] = 0;
        }
        for(i = 1; i <= n - m; i++) {
            if(!match[i] && !viz[i] && DFS(i)) {
                ok = 1;
                cuplaj++;
            }
        }
    }
    vector <pii> sol;
    for(i = 1; i <= n - m; i++) {
        if(match[i]) {
            sol.push_back({i, match[i] - (n - m)});
        }
    }
    cout << cuplaj << "\n";
    for(auto x : sol) {
        cout << x.first << " " << x.second << "\n";
    }
    return 0;
}