Cod sursa(job #303977)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 10 aprilie 2009 17:19:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <cstdio>
#include <vector>
using namespace std;
#define pb push_back
const int NMAX=10001;
vector <int> G[NMAX];
int N,M,E;
void read(){
     freopen("cuplaj.in","r",stdin);
     scanf("%d %d %d",&N,&M,&E);
     int x,y;
     while (E--){
           scanf("%d %d",&x,&y);
           G[x].pb(y);
           }
     }
int L[NMAX],R[NMAX];
bool U[NMAX];
int pairup(int vf){
    if (U[vf]) return 0;
    U[vf]=true;
    vector<int>::iterator it;
    for (it=G[vf].begin();it!=G[vf].end();++it)
      if (!L[*it]) { L[*it]=vf;
                     R[vf]=*it;
                     return 1; }
    for (it=G[vf].begin();it!=G[vf].end();++it)
      if (pairup(L[*it])) { L[*it]=vf;
                            R[vf]=*it;
                            return 1; }
    return 0;
    }
void solve(){
     int i;
     bool ok=true;
     memset(L,0,sizeof(L));
     memset(R,0,sizeof(R));
     while (ok){
        ok=false;
        memset(U,false,sizeof(U));
        for (i=1;i<=N;++i)
         if (!R[i])
          if (pairup(i)) ok=true;
        }
     }
void afis(){
     freopen("cuplaj.out","w",stdout);
     int i,nr=0;
     for (i=1;i<=N;++i) 
       if (R[i]) ++nr;
     printf("%d\n",nr);
     for (i=1;i<=N;++i)
       if (R[i]) printf("%d %d\n",i,R[i]);
     }
int main(){
    read();
    solve();
    afis();
    return 0;
    }