Cod sursa(job #3337593)

Utilizator uncrownedHojda Adelin uncrowned Data 29 ianuarie 2026 08:33:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <bits/stdc++.h>
using namespace std;

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

#define cin fin
#define cout fout

vector<int> g[10005];   
int l[10005];  
int r[10005]; 
bool v[10005];

int n, m, E;

bool dfs(int node) {
    if (v[node]) return false;
    v[node] = true;

    for (auto nxt : g[node]) {
        if (r[nxt] == 0) {
            l[node] = nxt;
            r[nxt] = node;
            return true;
        }
    }

    for (auto nxt : g[node]) {
        if (dfs(r[nxt])) {
            l[node] = nxt;
            r[nxt] = node;
            return true;
        }
    }

    return false;
}

int main() {
    cin >> n >> m >> E;

    for (int i = 0; i < E; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
    }

    bool change = true;
    while (change) {
        change = false;
        memset(v, 0, sizeof(v));

        for (int i = 1; i <= n; i++) {
            if (l[i] == 0) {
                if (dfs(i))
                    change = true;
            }
        }
    }

    int cuplaj = 0;
    for (int i = 1; i <= n; i++)
        if (l[i] != 0)
            cuplaj++;

    cout << cuplaj << "\n";
    for (int i = 1; i <= n; i++)
        if (l[i] != 0)
            cout << i << " " << l[i] << "\n";

    return 0;
}