Cod sursa(job #2059130)

Utilizator LizaSzabo Liza Liza Data 6 noiembrie 2017 18:02:57
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int NMax = 10000;
vector <int> G[NMax + 5];
int L[NMax + 5], R[NMax + 5], Use[NMax + 5];
int N,M,E,Cuplaj;

void Read()
{
  fin >> N >> M >> E;
  for(int i = 1; i <= E; ++i)
  {
    int x,y;
    fin >> x >> y;
    G[x].push_back(y);
  }
}

int Pairup(int Nod)
{
  if(Use[Nod])
    return 0;
  Use[Nod] = 1;

  for(int i = 0; i < (int)G[Nod].size(); ++i)
  {
    int Vecin = G[Nod][i];
    if(R[Vecin] == 0)
    {
      L[Nod] = Vecin;
      R[Vecin] = Nod;
      return 1;
    }
  }
  for(int i = 0; i < (int)G[Nod].size(); ++i)
  {
    int Vecin = G[Nod][i];
      if(Pairup(R[Vecin]) == 1)
      {
        L[Nod] = Vecin;
        R[Vecin] = Nod;
        return 1;
      }
  }
  return 0;
}

void Solve()
{
  int Ok;
  do
  {
    Ok = 0;
    memset(Use,0,sizeof(Use));
    for(int i = 1; i <= N; ++i)
      if(L[i] == 0)
        if(Pairup(i))
          {
            Cuplaj++;
            Ok = 1;
          }
  }
  while(Ok);
}

void Print()
{
  fout << Cuplaj << "\n";
  for(int i = 1; i <= N; ++i)
    if(L[i])
      fout << i << " " << L[i] << "\n";
}

int main()
{
    Read();
    Solve();
    Print();
    return 0;
}