Cod sursa(job #1336438)

Utilizator Pintilie_AndreiFII-Pintilie Andrei Pintilie_Andrei Data 7 februarie 2015 18:45:51
Problema Cuplaj maxim in graf bipartit Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>
#include <vector>
#define N 10004
using namespace std;
int n,m,e;
int u[N];
int L[N],R[N];
int cuplaj;
vector <int> a[N];
void Read()
{
    ifstream fin("cuplaj.in");

    fin>>n>>m>>e;
    int x,y;
    for(int i=1; i<=e; i++)
    {
        fin>>x>>y;
        a[x].push_back(y);
       // a[y].push_back(x);
    }
}

int Cupleaza(int x)
{
    if(u[x]) return 0;
    u[x]=1;
    int i;
    for(i=0; i<a[x].size(); i++)
    if(R[a[x][i]]==0) {R[a[x][i]]=x; L[x]=a[x][i]; return 1; }

    for(i=0; i<a[x].size(); i++)
    if(Cupleaza(R[a[x][i]])) {R[a[x][i]]=x; L[x]=a[x][i]; return 1;}

    return 0;
}

void Set0()
{
    for(int i=1; i<=m+n && i<=1000; i++)
        u[i]=0;
}

void Salv()
{
     ofstream fout("cuplaj.out");
  int i,ok=1;
  while(ok)
  {
      ok=0;
      Set0();
      for(i=1; i<=n; i++)
        if(L[i]==0)
      {

          ok+=Cupleaza(i);
      }

  }
  for(i=1; i<=n; i++)
    if(L[i]) cuplaj++;
  fout<<cuplaj<<"\n";
  for(i=1; i<=n; i++)
  if(L[i])
    fout<<i<<" "<<L[i]<<"\n";
}
int main()
{
    Read();
    Salv();
    return 0;
}