Cod sursa(job #1880392)

Utilizator remus88Neatu Remus Mihai remus88 Data 15 februarie 2017 18:43:41
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
// Cuplaj O(sqrt(l)*r) - Lanturi alternative
// In stanga am l noduri        In dreapta am r noduri
// In totatl m muchii
#include <fstream>
#include <vector>
#include <bitset>
#define Nmax 10009

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

int l,r,m,toleft[Nmax],toright[Nmax],x,y,sol;
vector < int > G[Nmax];
bitset < Nmax > viz;

bool Cupleaza(int node)
{
     if(viz[node])
        return 0;

     viz[node]=1;

     for(auto x : G[node])
          if(!toleft[x])
          {
               toright[node]=x;
               toleft[x]=node;
               return 1;
          }
     for(auto x : G[node])
          if(Cupleaza(toleft[x]))
          {
               toright[node]=x;
               toleft[x]=node;
               return 1;
          }
     return 0;
}

int main()
{
     f>>l>>r>>m;
     for(int i=1;i<=m;++i)
    {
        f>>x>>y;
        G[x].push_back(y);
    }
     int gata=0;
     while(!gata)
     {
          gata=1;
          for(int i=1;i<=l;++i)viz[i]=0;
          for(int i=1;i<=l;++i)
               if(!toright[i] && Cupleaza(i))
               {
                   ++sol;
                   gata=0;
               }
     }
     g<<sol<<'\n';
     for(int i=1;i<=l;++i)
          if(toright[i])g<<i<<' '<<toright[i]<<'\n';
     f.close();g.close();
     return 0;
}