Cod sursa(job #3337337)

Utilizator ioan19Buzdugan Ioan-Michael ioan19 Data 27 ianuarie 2026 11:23:03
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;
ifstream f("cuplaj.in");
ofstream h("cuplaj.out");

const int NMAX = 10005;
int N, M, E;
vector<int> g[NMAX];
vector<int> mt;
vector<bool> used;

bool try_kuhn(int v){
    if(used[v])
        return 0;
    used[v] = 1;
    for(int to : g[v])
        if(mt[to] == -1 || try_kuhn(mt[to])){
            mt[to] = v;
            return 1;
        }
    return 0;
}

int main() {
    if (!(f >> N >> M >> E)) return 0;
    for(int i = 1; i <= E; i++){
        int x, y;
        f >> x >> y;
        g[x].push_back(y);
    }

    mt.assign(M + 1, -1);
    vector<bool> used1(N + 1, 0);
    for(int i = 1; i <= N; i++)
        for(int to : g[i])
            if(mt[to] == -1){
                mt[to] = i;
                used1[i] = 1;
                break;
            }

    for(int i = 1; i <= N; i++){
        if(used1[i])
            continue;
        used.assign(N + 1, 0);
        try_kuhn(i);
    }

    int sol = 0;
    for (int i = 1; i <= M; ++i)
        if (mt[i] != -1)
            sol++;

    h << sol << "\n";
    for (int i = 1; i <= M; ++i)
        if (mt[i] != -1)
            h << mt[i] << " " << i << "\n";

    return 0;
}