Cod sursa(job #2526696)

Utilizator DariusDCDarius Capolna DariusDC Data 18 ianuarie 2020 22:58:12
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 10005;
int n, m, e;
vector <int> g[nmax];
int viz[nmax], st[nmax], dr[nmax];
int ok, ans;

inline bool check(int nod)
{
    if (viz[nod])
        return false;
    viz[nod] = 1;
    for (auto it : g[nod])
    {
        if (dr[it] == 0)
        {
            st[nod] = it;
            dr[it] = nod;
            return true;
        }
     }
     for (auto it : g[nod])
     {
         if (check(dr[it]))
         {
             st[nod] = it;
             dr[it] = nod;
             return true;
         }
     }
     return false;
}

int main()
{
    fin >> n >> m >> e;
    while (e--)
    {
        int a, b;
        fin >> a >> b;
        g[a].push_back(b);
    }
    ok = 1;
    while (ok)
    {
        ok = 0;
        for (int i = 1; i <= n; i++)
            viz[i] = 0;
        for (int i = 1; i <= n; i++)
            if (!st[i] && check(i))
            {
                ans++;
                ok = 1;
            }
    }
    fout << ans << "\n";
    for (int i = 1; i <= n; i++)
        if (st[i])
            fout << i << " " << st[i] << "\n";
    return 0;
}