Cod sursa(job #2113499)

Utilizator georgerapeanuRapeanu George georgerapeanu Data 24 ianuarie 2018 17:36:07
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <cstdio>
#include <vector>
#include <bitset>
using namespace std;
FILE *f=fopen("cuplaj.in","r");
FILE *g=fopen("cuplaj.out","w");
int N,M,K;
vector<int> G[10005];
int L[10005];
int R[10005];
bitset<10005> U;
bool pairup(int x){
   if(U[x]){
      return 0;
   }
   U[x] = 1;
   for(auto it:G[x]){
      if(!R[it]){
         R[it] = x;
         L[x] = it;
         return 1;
      }
   }
   for(auto it:G[x]){
      if(pairup(R[it])){
         R[it] = x;
         L[x] = it;
         return 1;
      }
   }
   return 0;
}
int cuplaj(){
   int c = 0;
   bool ok = 1;
   while(ok){
      ok = 0;
      U.reset();
      for(int i = 1;i <= N;i++){
         if(!L[i] && pairup(i)){
            ok = 1;
            c++;
         }
      }
   }
   return c;
}
int main()
{
    fscanf(f,"%d%d%d",&N,&M,&K);
    for(int i=1;i<=K;i++)
    {
        int x,y;
        fscanf(f,"%d%d",&x,&y);
        G[x].push_back(y);
    }
    fprintf(g,"%d\n",cuplaj());
    for(int i=1;i<=N;i++)
        if(L[i])
            fprintf(g,"%d %d\n",i,L[i]);
    fclose(f);
    fclose(g);
    return 0;
}