Cod sursa(job #2709570)

Utilizator isa_tudor_andreiAndrei Tudor isa_tudor_andrei Data 20 februarie 2021 14:04:36
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int NMAX = 10000;
const int MMAX = 10000;
int l[NMAX + 5], r[MMAX + 5], viz[NMAX + 5];
vector <int> G[NMAX + 5];
int path( int u ) {
  if( viz[u] == 1 )
    return 0;
  viz[u] = 1;
  for( int j = 0 ; j < G[u].size() ; j ++ ) {
    int v = G[u][j];
    if( !r[v] || path( r[v] ) ) {
      l[u] = v;
      r[v] = u;
      return 1;
    }
  }
  return 0;
}
int main() {
  freopen( "cuplaj.in", "r", stdin );
  freopen( "cuplaj.out", "w", stdout );
  int n, m, e, x, y, i, cuplaj, gasit;
  scanf( "%d%d%d", &n, &m, &e );
  for( i = 1 ; i <= e ; i ++ ) {
    scanf( "%d%d", &x, &y );
    G[x].push_back( y );
  }
  cuplaj = 0;
  do {
    gasit = 0;
    memset( viz, 0, sizeof( viz ) );
    for( i = 1 ; i <= n ; i ++ )
      if( !l[i] && path( i ) ) {
        gasit = 1;
        cuplaj ++;
      }
  } while( gasit );
  printf( "%d\n", cuplaj );
  for( i = 1 ; i <= n ; i ++ )
    if( l[i] )
      printf( "%d %d\n", i, l[i] );
  return 0;
}