Cod sursa(job #2679262)

Utilizator valentinchipuc123Valentin Chipuc valentinchipuc123 Data 30 noiembrie 2020 02:54:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");

int n,m,q,l[10005],r[10005];
bool fost[10005];
vector<int>vecini[10005];

int imperecheaza(int k)
{
    if(fost[k]) return 0;
    fost[k]=1;

    for(int i=0; i<vecini[k].size(); i++)
    {
        int cap=vecini[k][i];
        if(!r[cap])
        {
            l[k]=cap;
            r[cap]=k;
            return 1;
        }
    }
    for(int i=0; i<vecini[k].size(); i++)
    {
        int cap=vecini[k][i];
        if(imperecheaza(r[cap]))
        {
            l[k]=cap;
            r[cap]=k;
            return 1;
        }
    }
    return 0;
}

int main()
{
    f>>n>>m>>q;
    for(int i=1; i<=q; i++)
    {
        int x,y;
        f>>x>>y;
        vecini[x].push_back(y);
    }
    bool schimbare=1;
    while( schimbare )
    {
        schimbare=0;
        for(int i=1; i<=n; i++) fost[i]=0;
        for(int i=1; i<=n; i++) if(!l[i]) schimbare|=imperecheaza(i);
    }
    int cuplaj=0;
    for(int i=1; i<=n; i++) cuplaj+=(l[i]>0);
    g<<cuplaj<<'\n';
    for(int i=1; i<=n; i++) if(l[i]>0) g<<i<<' '<<l[i]<<'\n';
}