Cod sursa(job #2691403)

Utilizator speedypleathGheorghe Andrei speedypleath Data 28 decembrie 2020 15:09:24
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
int n,m,source,dest,l[10005],r[10005],n1,n2,viz[10005],ans;
vector<int> graf[10005];


bool match(int node)
{
    if (viz[node])
        return false;
    viz[node] = true;
    for (auto it: graf[node])
    {
        if (r[it] == -1)
        {
            l[node] = it;
            r[it] = node;
            return true;
        }
    }

    for (auto it: graf[node])
    {
        if (match(r[it]))
        {
            l[node] = it;
            r[it] = node;
            return true;
        }
    }
    return false;
  }


int main()
{
    memset(l,-1,sizeof(l));
    memset(r,-1,sizeof(r));
    in>>n1>>n2>>m;
    for(int i=0;i<m;i++)
    {
        int a,b;
        in>>a>>b;
        a--;b--;
        graf[a].push_back(b);
    }
    int ok = 1;
    while (ok)
    {
        ok = 0;
        memset(viz,0,sizeof(viz));
        for(int i=0;i<n1;i++)
            if (l[i] == -1 && match(i)){
                ok = 1;
                ans++;
            }
    }
    out<<ans<<'\n';
    for(int i=0;i<n1;i++)
        if(l[i] != -1)
            out<<i+1<<' '<<l[i]+1<<endl;

    return 0;
}