Cod sursa(job #3042093)

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

#define MMAX 10008

using namespace std;

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

bool cuplat[MMAX], used[MMAX];
int match[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] == -1 || 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);
    }

    for(int i = 1; i <= m; i ++)
        match[i] = -1;

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

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

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

    return 0;
}