Cod sursa(job #1557508)

Utilizator PTAdrian64Pop-Tifrea Adrian PTAdrian64 Data 27 decembrie 2015 17:31:51
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1 kb
#include <cstdio>
#include <vector>
#define nmax 10005
#include <cstring>

using namespace std;

int N,M,E,C = 0;
vector <int> G[nmax];
int u[nmax],l[nmax],r[nmax];

bool pairup(int x){

  for(vector <int> :: iterator it = G[x].begin();it != G[x].end();++it)
    if(r[*it] == 0){
      l[x] = *it;
      r[*it] = x;
      return true;
    }

  for(vector <int> :: iterator it = G[x].begin();it != G[x].end();++it)
    if(pairup(r[*it])){
      l[x] = *it;
      r[*it] = x;
      return true;
    }

   return false;
}

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

   scanf("%d %d %d ",&N,&M,&E);
   int x,y;
   while(E--){
     scanf("%d %d ",&x,&y);
     G[x].push_back(y);
   }

   bool ok = true;
   do{
     ok = false;

     memset(u,0,sizeof(u));

     for(x = 1;x <= N;++x)
       if(l[x] == 0 )ok |= pairup(x);

   }while(ok == true);

   for(x = 1;x <= N;++x)C += (l[x] > 0);

   printf("%d\n",C);
   for(x = 1;x <= N;++x)if(l[x])printf("%d %d\n",x,l[x]);
    return 0;
}