Cod sursa(job #2604124)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 21 aprilie 2020 18:05:33
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

#define NMAX 10005
using namespace std;

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

vector<int> G[2 * NMAX];
int st[2 * NMAX], dr[2 * NMAX];
bool viz[2 * NMAX];

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()
{
    int n, m, k;
    fin >> n >> m >> k;

    for(int i = 1; i <= k; ++i)
    {
        int x, y;
        fin >> x >> y;

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

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

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