Cod sursa(job #2739490)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 8 aprilie 2021 13:35:11
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>

using namespace std;

const int INF(1 << 30), NMAX(10005);
using VI  = vector<int>;
using VVI = vector<VI>;
using VB  = vector<bool>;

void BUNA(const string& task = "")
{
    if (!task.empty())
        freopen((task + ".in").c_str(), "r", stdin),
                freopen((task + ".out").c_str(), "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
void PA()
{
    exit(0);
}
vector<int> G[2 * NMAX];
int st[2 * NMAX], dr[2 * NMAX], viz[2 * NMAX];

inline bool cup(int nod)
{
    viz[nod] = 1;
    for(auto it: G[nod])
        if(!dr[it])
        {
            st[nod] = it;
            dr[it] = nod;
            return 1;
        }

    for(auto it: G[nod])
        if(!viz[dr[it]] && cup(dr[it]))
        {
            st[nod] = it;
            dr[it] = nod;
            return 1;
        }
    return 0;
}

int main()
{
    BUNA("cuplaj");
    int n, m, e;
    cin >> n >> m >> e;

    for(int i = 1; i <= e; ++i)
    {
        int x, y;
        cin >> x >> y;

        G[x].emplace_back(y + NMAX);
        G[y + NMAX].emplace_back(x);
    }

    bool ok = 0;
    do
    {
        ok = 0;
        memset(viz, 0, sizeof(viz));
        for(int i = 1; i <= n; ++i)
            if(!viz[i] && !st[i])
                ok |= cup(i);
    }
    while(ok);

    int cnt = 0;
    for(int i = 1; i <= n; ++i)
        if(st[i])
            ++cnt;

    cout << cnt << '\n';
    for(int i = 1; i <= n; ++i)
        if(st[i])
            cout << i << ' ' << st[i] - NMAX << '\n';
    PA();
    return 0;
}