Cod sursa(job #2523480)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 14 ianuarie 2020 10:44:40
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>
#define NM 10005
using namespace std;
void read();
int n, m, e, ma[2*NM], cmax;
vector<int> v[2*NM];
set<int> L, R;
bitset<2*NM> viz;
bool find_match(int nod)
{
    if(viz[nod])
        return 0;
    viz[nod] = 1;
    for(auto it:v[nod])
        if(!ma[it])
        {
            ma[nod] = it;
            ma[it] = nod;
            return 1;
        }
    for(auto it:v[nod])
        if(ma[it] && find_match(ma[it]) && ma[it]!=nod)
        {
            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++;
    printf("%d\n", cmax);
    for(auto it:L)
        if(ma[it])
        {
            printf("%d", it);
            printf(" ");
            printf("%d\n", ma[it]-n+m);
        }
    return 0;
}
void read()
{
    freopen("cuplaj.in", "r", stdin);
	freopen("cuplaj.out", "w", stdout);
	scanf("%d%d%d", &n, &m, &e);
    while(e--)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        y+=n;
        L.insert(x);
        R.insert(y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    n+=m;
}