Cod sursa(job #2709577)

Utilizator isa_tudor_andreiAndrei Tudor isa_tudor_andrei Data 20 februarie 2021 14:31:43
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 10000;

vector<int> G[2*NMAX + 1];
int viz[2*NMAX + 1], p[2*NMAX + 1];

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

  viz[node] = 1;
  for( const auto &it : G[node] ) {
    if( !p[it] || path(p[it]) ) {
      p[node] = it;
      p[it] = node;
      return 1;
    }
  }
  return 0;
}

int main() {
  int n, m, e;
  fin>>n>>m>>e;

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

  int cuplaj = 0, gasit;
  do{
    gasit = 0;
    memset(viz, 0, sizeof(viz));
    for( int i = 1; i <= n; i ++ ) {
      if( !p[i] && path(i) ) {
        cuplaj ++;
        gasit = 1;
      }
    }
  }while( gasit );

  fout<<cuplaj<<"\n";
  for( int i = 1; i <= n; i ++ )
    if( p[i] )
      fout<<i<<" "<<p[i] - n<<"\n";
  return 0;
}