Cod sursa(job #2807486)

Utilizator As932Stanciu Andreea As932 Data 23 noiembrie 2021 20:52:44
Problema Cuplaj maxim de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 1e4 + 5;

int n, m, e, l[nmax], r[nmax];
bool vis[nmax];
vector <int> v[nmax];

void read(){
    fin >> n >> m >> e;
    for(int i = 1; i <= e; i++){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
    }
}

bool mk_pair(int x){
    if(vis[x])
        return false;

    vis[x] = true;

    for(auto y:v[x])
        if(!l[y]){
            l[y] = x;
            r[x] = y;
            return true;
        }

    for(auto y:v[x])
        if(mk_pair(l[y])){
            l[y] = x;
            r[x] = y;
            return true;
        }

    return false;
}

void solve(){
    bool ok = false;
    int cnt = 0;

    do {
        ok = false;
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++)
            if(!r[i] && mk_pair(i)){
                ok = true;
                cnt++;
            }
    } while(ok);

    fout << cnt << "\n";

    for(int i = 1; i <= n; i++)
        if(r[i])
            fout << i << " " << r[i] << "\n";
}

int main()
{
    read();
    solve();

    return 0;
}