Cod sursa(job #2142110)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 24 februarie 2018 18:58:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

const int N = 1e4 + 5;
int L[N], R[N];
bool used[N];
vector <int> edge[N];

bool match(int x){
    if(used[x] == true){
        return false;
    }
    used[x] = true;
    for(auto &it : edge[x]){
        if(R[it] == 0){
            L[x] = it;
            R[it] = x;
            return true;
        }
    }
    for(auto &it : edge[x]){
        if(match(R[it])){
            L[x] = it;
            R[it] = x;
            return true;
        }
    }
    return false;
}

int main(){
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    int n, m, e;
    scanf("%d %d %d", &n, &m, &e);
    for(int i = 1;i <= e;i++){
        int x, y;
        scanf("%d %d", &x, &y);
        edge[x].push_back(y);
    }
    bool found = true;
    int ans = 0;
    while(found){
        found = false;
        for(int i = 1;i <= n;i++){
            if(L[i] == 0){
                if(match(i)){
                    found = true;
                    ans++;
                }
            }
        }
        for(int i = 1;i <= n;i++){
            used[i] = 0;
        }
    }
    printf("%d\n", ans);
    for(int i = 1;i <= n;i++){
        if(L[i]){
            printf("%d %d\n", i, L[i]);
        }
    }
    return 0;
}