Cod sursa(job #1449652)

Utilizator PTAdrian64Pop-Tifrea Adrian PTAdrian64 Data 10 iunie 2015 10:13:07
Problema Cuplaj maxim in graf bipartit Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <cstdio>
#include <vector>
#define nmax 10005
#include <cstring>

using namespace std;

vector <int> graf[nmax];
int l[nmax],r[nmax];
bool u[nmax];
int n1,n2;

bool imperecheaza(int n){
    if(u[n]==true)return false;
    u[n]=true;
    for(vector <int>::iterator it=graf[n].begin();it!=graf[n].end();++it)if(r[*it]==false){
        l[n] = *it;
        r[*it] = n;
        return true;
    }
    for(vector <int>::iterator it=graf[n].begin();it!=graf[n].end();++it)if(imperecheaza(r[*it])==true){
        l[n] = *it;
        r[*it] = n;
        return true;
    }
    return false;
}
void citeste(){
    int m,x,y;
    scanf("%d %d %d ",&n1,&n2,&m);
    while(m--){
        scanf("%d %d ",&x,&y);
        graf[x].push_back(y);
    }
}
void rezolva(){
    bool c;
    int i;
    do{
        c=false;
        memset(u,false,sizeof(u));
        for(i=1;i<=n1;i++)if(!l[i])c=imperecheaza(i);
    }while(c);
    int res=0;
    for(i=1;i<=n1;i++)if(l[i])res++;
    printf("%d\n",res);
    for(i = 1;i<=n1;i++)if(l[i])printf("%d %d\n",i,l[i]);
}
int main(){
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    citeste();
    rezolva();
    return 0;
}