Cod sursa(job #1747778)

Utilizator Dan_RadulescuRadulescu Dan Dan_Radulescu Data 25 august 2016 16:17:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n,m,e,i,sw,st[10001],dr[10001],v[10001],nrm;
struct nod{
   int inf;
   nod *urm;
}*L[10001];
void adaugsf(nod *&vf,int val){
     nod *q;
     q=new nod;
     q->inf=val;
     q->urm=vf;
     vf=q;
}
void cit(){
    fin>>n>>m>>e;
    int i,a,b;
    for (i=1;i<=e;i++){
        fin>>a>>b;
        adaugsf(L[a],b);
    }
    fin.close();
}
int cuplaj(int nd){
    nod *q;
    if (v[nd]==1) return 0;
    v[nd]=1;
    q=L[nd];
    while(q!=0){
        if (dr[q->inf]==0){
            dr[q->inf]=nd;
            st[nd]=q->inf;
            return 1;
        }
        q=q->urm;
    }
    q=L[nd];
    while(q!=0){
        if (cuplaj(dr[q->inf])==1){
            dr[q->inf]=nd;
            st[nd]=q->inf;
            return 1;
        }
        q=q->urm;
    }
    return 0;
}
int main(){
    cit();
    while(sw==0){
        sw=1;
        memset(v,0,sizeof(v));
        for (i=1;i<=n;i++)
          if (st[i]==0 && cuplaj(i)==1) {
              nrm++;
              sw=0;
          }
    }
    fout<<nrm<<'\n';
    for (i=1;i<=n;i++)
        if (st[i]!=0) fout<<i<<" "<<st[i]<<'\n';
    fout.close();
    return 0;
}