Cod sursa(job #228450)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 7 decembrie 2008 11:55:37
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <cstdio>
#include <vector>
using namespace std;
const int NMAX=10001;
vector<int> a[NMAX];
int N,M,E,s[NMAX],d[NMAX];
bool u[NMAX],ok=true;
int pairup(int x){
    if (u[x]) return 0;
    u[x]=true;
    vector<int>::iterator it;
    for (it=a[x].begin();it!=a[x].end();++it)
     if (s[*it]==0){
       s[*it]=x;
       d[x]=*it;
       return 1;}
    for (it=a[x].begin();it!=a[x].end();++it)
     if (pairup(s[*it])){
       s[*it]=x;
       d[x]=*it;
       return 1;}
    return 0;
} 
int main(){
    int i,j;
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d %d",&N,&M,&E);
    while (E--){
      scanf("%d %d",&i,&j);
      a[i].push_back(j);
      }
    while (ok){
      ok=false;
      memset(u,false,sizeof(u));
      for (i=1;i<=N;++i)
       if (d[i]==0)
        if (pairup(i)) ok=true;
      }
    for (i=1,j=0;i<=N;++i)
     if (d[i]>0) ++j;
    printf("%d\n",j);
    for (i=1;i<=N;++i)
     if (d[i]>0)
      printf("%d %d\n",i,d[i]);
    return 0;
}