Cod sursa(job #2930619)

Utilizator DooMeDCristian Alexutan DooMeD Data 28 octombrie 2022 22:05:22
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e4;

vector<vector<int>> dx(nmax+5);
bool viz[nmax+5];
int st[nmax+5], dr[nmax+5];

bool couple(int node) {
    if(viz[node]) return false; 
    viz[node] = true;
    for(auto next : dx[node]) 
        if(dr[next] == 0) {
            st[node] = next;
            dr[next] = node;
            return true;
        }
    for(auto next : dx[node])
        if(couple(dr[next])) {
            st[node] = next;
            dr[next] = node;
            return true;
        }
    return false;
}

int main() {
    ifstream f("cuplaj.in");
    ofstream g("cuplaj.out");

    int n, m, e; f >> n >> m >> e;
    for(int i=1; i<=e; i++) {
        int x, y; f >> x >> y;
        dx[x].emplace_back(y);
    }
    int ans = 0;
    /*bool ok = false;
    while(ok == false) {
        ok = true;
        for(int i=1; i<=n; i++) viz[i] = false;
        for(int i=1; i<=n; i++)
            if(st[i] == 0 and couple(i)) {
                ans++;
                ok = false;
            }
    }*/
    for(int i=1; i<=n; i++)
        if(st[i] == 0) {
            if(!couple(i)) {
                for(int j=1; j<=n; j++) viz[j] = false;
                ans += couple(i);
            }
            else ans++;
        }
    g << ans << "\n";
    for(int i=1; i<=n; i++)
        if(st[i]) g << i << " " << st[i] << "\n";
    return 0;
}