Cod sursa(job #2523463)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 14 ianuarie 2020 10:27:02
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
#define NM 10005
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
void read();
int n, m, e, ma[NM], cmax;
vector<int> v[NM];
set<int> L, R;
bitset<NM> viz;
bool find_match(int nod)
{
    if(viz[nod])
        return 0;
    viz[nod] = 1;
    for(auto it:v[nod])
        if(!ma[it] || (ma[it] && find_match(ma[it])))
        {
            ma[nod] = it;
            ma[it] = nod;
            return 1;
        }
    return 0;
}
int main()
{
    read();
    bool ok = 1;
    while(ok)
    {
        ok = 0;
        for(int i=1; i<=n; i++)
            viz[i] = 0;
        for(auto it:L)
            if(!ma[it] && find_match(it))
                ok = 1;
    }
    for(auto it:L)
        if(ma[it])
            cmax++;
    fout << cmax << '\n';
    for(auto it:L)
        if(ma[it])
            fout << it << ' ' << ma[it]-n+m << '\n';
    return 0;
}
void read()
{
    fin >> n >> m >> e;
    while(e--)
    {
        int x, y;
        fin >> x >> y;
        y+=n;
        L.insert(x);
        R.insert(y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    n+=m;
}