Cod sursa(job #2570485)

Utilizator Cezar211Popoveniuc Cezar Cezar211 Data 4 martie 2020 17:07:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
#define NM 10005
using namespace std;
ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");
int N, M, muchii;
int ma[2*NM];
bitset<2*NM> viz;
vector<int> v[2*NM];
bool dfs(int nod)
{
    viz[nod] = 1;
    for(auto it:v[nod])
        if(!ma[it] || (!viz[ma[it]] && dfs(ma[it])))
        {
            ma[nod] = it;
            ma[it] = nod;
            return 1;
        }
    return 0;
}
void read();
int main()
{
    read();
    bool ok = 1;
    while(ok)
    {
        ok = 0;
        for(int i=1; i<=2*N; i++)
            viz[i] = 0;
        for(int i=1; i<=N; i++)
            if(!ma[i] && !viz[i] && dfs(i))
                ok = 1;
    }
    int nr = 0;
    for(int i=1; i<=N; i++)
        if(ma[i])
            nr++;
    fout << nr << '\n';
    for(int i=1; i<=N; i++)
        if(ma[i])
            fout << i << ' ' << ma[i]-N << '\n';
    return 0;
}
void read()
{
    fin >> N >> M >> muchii;
    while(muchii--)
    {
        int x, y;
        fin >> x >> y;
        v[x].push_back(y+N);
        v[y+N].push_back(x);
    }
}