Cod sursa(job #648005)

Utilizator vendettaSalajan Razvan vendetta Data 12 decembrie 2011 21:27:55
Problema Cuplaj maxim in graf bipartit Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <vector>
#include <bitset>
#define nmax 10001

using namespace std;

vector<int> gf[nmax];
int n, m, e;
int st[nmax], dr[nmax];
bitset<nmax> viz;
int card_cup;//cardinalul cuplajului;

void citeste(){

   scanf("%d%d%d", &n, &m, &e);
   for(int i=1; i<=e; ++i){
      int x, y;
      scanf("%d %d", &x, &y);
      gf[x].push_back(y);
   }

}

bool cupleaza(int nod){

   if (viz[nod]!=0) return 0;
   viz[nod] = 1;
   for(unsigned i=0; i < gf[nod].size(); ++i){
      int vc = gf[nod][i];
      if (dr[vc] == 0 || cupleaza(dr[vc])!=0 ){
         st[nod] = vc;
         dr[vc] = nod;
         return 1;
      }
   }
   return 0;
}

void rezolva(){

   for(int i=1; i<=n; ++i){
      if (st[i] != 0) continue;//daca nodul i e conectat;
      for(int j=0; j<=nmax; ++j) viz[j] = 0;
      if (cupleaza(i)!=0) ++card_cup;
   }

}

void scrie(){

   printf("%d\n", card_cup);
   for(int i=1; i<=n; ++i){
      if (st[i]) printf("%d %d\n", i, st[i] );
   }

}

int main(){

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

   citeste();
   rezolva();
   scrie();

   return 0;

}