Cod sursa(job #2144986)

Utilizator infomaxInfomax infomax Data 27 februarie 2018 00:41:07
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

ifstream F("cuplaj.in");
ofstream G("cuplaj.out");

int n, m, k, x, y, right_pair[10005], left_pair[10005], ans,ok;
vector<int> a[10005];
bitset<10005> v;

int Match(int x){
    v[x] = 1;
    for(auto it:a[x])
        if(!left_pair[it]){
            ans++;
            left_pair[it]=x;
            right_pair[x]=it;
            return 1;
        }
    for(auto it:a[x])
        if(!v[left_pair[it]]&&Match(left_pair[it])){ ///dc pot reface legaturile
            left_pair[it]=x;
            right_pair[x]=it;
            return 1;
        }
    return 0;
}

int main()
{
    F >> n >> m >> k;
    for(int i = 1; i <= k; ++ i){
        F >> x >> y;
        a[x].push_back(y);
    }
    while(!ok){
        ok = 1;
        v = 0;
        for(int i = 1; i <= n; ++ i)
            if(!right_pair[i]&&Match(i)) ok=0;///dc nodul curent nu are pereche si nu termin de cuplat trebuie sa continui;
    }
    G << ans << '\n';
    for(int i = 1; i <= n; ++ i)
        if(right_pair[i]) G << i << " " << right_pair[i] << '\n';
    return 0;
}