Cod sursa(job #2593917)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 4 aprilie 2020 21:32:25
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <cstdio>
#include <vector>
#define NMAX 10000

using namespace std;

bool uz[NMAX + 5];
int n, m, e;
int l[NMAX + 5], r[NMAX + 5];
vector<int> vl[NMAX + 5], vr[NMAX + 5];

bool alternating_path(int nod) { /// true daca exista un lant alternant care pleaca din nod
  uz[nod] = true;
  for(int x: vl[nod])
    if(r[x] == 0) {
      l[nod] = x;
      r[x] = nod;
      return 1;
    }
  for(int x: vl[nod])
    if(!uz[r[x]] && r[x] != nod && alternating_path(r[x])) {
      l[nod] = x;
      r[x] = nod;
      return true;
    }
  return false;
}

int main() {
  freopen("cuplaj.in", "r", stdin);
  freopen("cuplaj.out", "w", stdout);
  int x, y;

  scanf("%d %d %d", &n, &m, &e);
  while(e) {
    scanf("%d %d", &x, &y);
    vl[x].push_back(y);
    vr[y].push_back(x);
    e--;
  }

  int cuplaj = 0;

  for(int i = 1; i <= n; i++) {
    for(int i = 1; i <= n; i++)
      uz[i] = false;
    if(l[i] == 0 && alternating_path(i)) {
      cuplaj++;
      continue;
    }
  }

  printf("%d\n", cuplaj);
  for(int i = 1; i <= n; i++)
    if(l[i] != 0)
      printf("%d %d\n", i, l[i]);

  return 0;
}