Cod sursa(job #1157219)

Utilizator beldeabogdanBogdan Beldea beldeabogdan Data 28 martie 2014 12:35:09
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <cstdio>
#include <vector>
#define nmax 100005
using namespace std;

vector <int> g[nmax];
int c1[nmax];
int c2[nmax];
bool viz[nmax];
int n,m,e;

bool cuplaj(int x) {
    if (viz[x]) return false;
    viz[x] = true;
    for (vector <int> :: iterator it = g[x].begin();it != g[x].end();it++) {
       if (c2[*it] == 0) {
           c2[*it] = x;
           c2[c1[x]]= 0;
           c1[x]= *it;
           return true;
       }
    }
    for (vector <int> :: iterator it = g[x].begin();it != g[x].end();it++) {
        if (cuplaj(c2[*it])) {
            c1[x] = *it;
            c2[*it] = x;
            return true;
        }
    }
    return false;
}

int main() {
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d %d",&n,&m,&e);
    for (int i=1;i<=e;i++) {
        int a,b;
        scanf("%d %d",&a,&b);
        g[a].push_back(b);
    }
    bool cuplat = true;
    while (cuplat) {
        cuplat = false;
        for (int j=1;j<=n;j++) viz[j] = false;
        for (int i=1;i<=n;i++) {
            if (c1[i] == 0) {
                if (cuplaj(i)) cuplat = true;
            }
        }
    }
    for (int i=1;i<=n;i++) {
        if (c1[i] != 0) printf("%d %d\n",i,c1[i]);
    }
    return 0;
}