Cod sursa(job #1974170)

Utilizator ctrlNBDorel Andrescu ctrlNB Data 26 aprilie 2017 23:56:29
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <climits>
#include <bitset>
 
using namespace std;
 
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
 
int n,m,e;
int N;
vector<int> E[10003];
int L[10003];
int R[10003];
bitset<10003> u;
 
bool pairup(int nod) {
 
    if(u[nod])
        return 0;
    u[nod] = 1;
    //cout << nod << " " << L[nod] << '\n';
 
    for(int i = 0; i < E[nod].size(); i++) {
        if(R[E[nod][i]] == 0) {
            L[nod] = E[nod][i];
            R[E[nod][i]] = nod;
            return 1;
        }
    }
 
    for(int i = 0; i < E[nod].size(); i++) {
        if(pairup(R[E[nod][i]])) {
            L[nod] = E[nod][i];
            R[E[nod][i]] = nod;
            return 1;
        }
    }
 
    return 0;
 
}
 
int main() {
 
    in >> n >> m >> e;
    int x,y;
 
    for(int i = 1; i <= e; i++) {
        in >> x >> y;
        E[x].push_back(y);
        E[y].push_back(x);
    }
 
    int cup = 0;
    bool f = true;
 
    while(f) {
 
        f = false;
 
        for(int j = 1; j <= n; j++)
            u[j] = 0;
        for(int i = 1; i <= n; i++) {
            if(L[i] != 0)
                continue;
            if(pairup(i)) {
                f = true;
                cup++;
            }
        }
 
    }
 
    out << cup << '\n';
 
    for(int i = 1; i <= n; i++) {
        if(L[i] == 0)
            continue;
        out << i << " " << L[i] << '\n';
    }
 
    return 0;
}