Cod sursa(job #1883636)

Utilizator stefan_gheorgheGheorghe Stefan stefan_gheorghe Data 18 februarie 2017 09:47:11
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int l,r,m,lf[10009],rg[10009],x,y,sol,use[10009];
vector < int > a[10009];

bool cupleaza(int node)
{
     if(use[node])
        return 0;
    use[node]=1;
     for(auto x : a[node])
          if(! lf[x])
          {
               rg[node]=x;
               lf[x]=node;
               return 1;
          }
     for(auto x : a[node])
          if(cupleaza(lf[x]))
          {
               rg[node]=x;
               lf[x]=node;
               return 1;
          }
     return 0;
}

int main()
{
     f>>l>>r>>m;
     for(int i=1;i<=m;++i)
    {
        f>>x>>y;
        a[x].push_back(y);
    }
     int ok=0;
     while(!ok)
     {
          ok=1;
          for(int i=1;i<=l;++i)use[i]=0;
          for(int i=1;i<=l;++i)if(!rg[i] && cupleaza(i))++sol,ok=0;
     }
     g<<sol<<'\n';
     for(int i=1;i<=l;++i)
          if(rg[i])g<<i<<' '<<rg[i]<<'\n';
     return 0;
}