Cod sursa(job #2417573)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 30 aprilie 2019 14:23:21
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>
#define maxn 10000
#define pb push_back
#define aaa system("pause");

using namespace std;

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

vector <int> g[2*maxn+5];
int bip[2*maxn+5];
bool viz[2*maxn+5];
int n, m, csz;

int cuplaj ( int nod )
{
  int i, nn;
  for ( i = 0; i < (int)g[nod].size(); i++ )
  {
    nn = g[nod][i];
    if ( bip[nn] == 0 ) { bip[nod] = nn; bip[nn] = nod; return 1; }
  }
  viz[nod] = 1;
  for ( i = 0; i < (int)g[nod].size(); i++ )
  {
    nn = g[nod][i];
    if ( viz[bip[nn]] == 0 && cuplaj (bip[nn]) == 1 ) { bip[nod] = nn; bip[nn] = nod; return 1; }
  }
  return 0;
}

int main ()
{
  int e; fin >> n >> m >> e;
  int i, j, z, a, b, aml = 1;
  for ( i = 1; i <= e; i++ ) fin >> a >> b, g[a].pb(n+b), g[n+b].pb(a);
  while ( aml == 1 )
  {
    aml = 0; memset (viz, 0, sizeof viz);
    for ( i = 1; i <= n; i++ )
      if ( bip[i] == 0 ) aml |= cuplaj (i);
  }
  for ( i = 1; i <= n; i++ ) csz += (bip[i]>0);
  fout << csz << '\n';
  for ( i = 1; i <= n; i++ )
    if ( bip[i] > 0 ) fout << i << ' ' << bip[i]-n << '\n';

  fin.close (); fout.close ();
  return 0;
}