Cod sursa(job #2572066)

Utilizator DariusDCDarius Capolna DariusDC Data 5 martie 2020 11:27:25
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

using namespace std;

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

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

inline void read()
{
    fin >> n >> m >> M;
    while (M--)
    {
        int a, b;
        fin >> a >> b;
        g[a].push_back(b);
    }
}

inline bool check(int nod)
{
    if (viz[nod])
        return false;
    viz[nod] = 1;
    for (unsigned int i = 0; i < g[nod].size(); i++)
    {
        int vecin = g[nod][i];
        if (!dr[vecin])
        {
            st[nod] = vecin;
            dr[vecin] = nod;
            return true;
        }
    }
    for (unsigned int i = 0; i < g[nod].size(); i++)
    {
        int vecin = g[nod][i];
        if (check(dr[vecin]))
        {
            st[nod] = vecin;
            dr[vecin] = nod;
            return true;
        }
    }
    return false;
}

inline void cupleaza()
{
    int ok = 0;
    int ans = 0;
    while (!ok)
    {
        ok = 1;
        memset(viz, 0, sizeof(viz));
        for (int i = 1; i <= n; i++)
            if (!st[i] && check(i))
                ans++, ok = 0;
    }
    fout << ans << "\n";
    for (int i = 1; i <= n; i++)
        if (st[i])
            fout << i << " " << st[i] << "\n";
}

int main()
{
    read();
    cupleaza();
    return 0;
}