Cod sursa(job #612997)

Utilizator theodora_maneaManea Theodora Maria theodora_manea Data 14 septembrie 2011 12:55:10
Problema Cuplaj maxim in graf bipartit Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <vector>
#include <cstring>

#define max_n 10001

using namespace std;

int n1,n2,e,nr,i,x,y;
vector <int> g[max_n];
bool ok[max_n];
int st[max_n],dr[max_n];

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

int cupleaza(int x) {
   int i;
   vector <int>::iterator it;
   if (ok[x]) return 0;
   ok[x]=true;
   for (it=g[x].begin(); it!=g[x].end(); it++) {
       i=*it;
       if (!dr[i]) {
          st[x]=i;
          dr[i]=x;
          return 1;
          }
   }
   for (it=g[x].begin(); it!=g[x].end(); it++) {
       i=*it;
       if (cupleaza(dr[i])) {
          st[x]=i;
          dr[i]=x;
          return 1;
        }
   }
   return 0;

}


void solve() {
    int i;
     for (i=1; i<=n1; i++) {
       if (!st[i]) {
          if (cupleaza(i)) nr++;
          else {
             memset(ok,false,sizeof(ok));
             if (cupleaza(i)) nr++;
          }
       }
   }
}

int main () {
   in >> n1 >> n2 >> e;
   for (i=1; i<=e; i++) {
       in >> x >> y;
       g[x].push_back(y);
   }

   memset(st,0,sizeof(st));
   memset(dr,0,sizeof(dr));
   memset(ok,false,sizeof(ok));

   nr=0;
   solve();

   out << nr << '\n';
   for (i=1; i<=n1; i++)
       if (st[i]>0) out << i << ' ' << st[i] << '\n';
    return 0;

}