Cod sursa(job #3042083)

Utilizator SmauSmau George Robert Smau Data 3 aprilie 2023 21:44:45
Problema Cuplaj maxim in graf bipartit Scor 8
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

#define MMAX 100//08

using namespace std;

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

bool cuplat[MMAX];
int match[MMAX];
vector <bool> used(MMAX);

vector <int> G[MMAX];

bool DFS(int node) {
    if(used[node]) return false;
    used[node] = true;
    
    for(auto x : G[node])
        if(!match[x] || DFS(match[x])) {
            match[x] = node;
            cuplat[node] = true;

            return true;
        }

    return false;
}

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(NULL); fout.tie(NULL);
    int n, m, e; fin >> n >> m >> e;
    for(int i = 1; i <= e; i ++) {
        int u, v; fin >> u >> v;
        G[u].push_back(v);
    }

    bool found = false;
    while(!found) {
        found = true;
        used = vector <bool> (MMAX, 0);
        for(int i = 1; i <= n; i ++)
            if(!cuplat[i]) found = !DFS(i);
    }

    int ans = 0;
    for(int i = 1; i <= m; i ++) ans += (match[i] != 0);

    fout << ans << '\n';
    for(int i = 1; i <= m; i ++)
        if(match[i] != 0) fout << i << ' ' << match[i] << '\n';

    return 0;
}