Cod sursa(job #1561371)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 3 ianuarie 2016 23:42:13
Problema Cuplaj maxim in graf bipartit Scor 28
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

const int NMax = 1e4 + 5;

int A[NMax], B[NMax];
bool Used[NMax];

vector < int > G[NMax];

bool Match(const int &x){
    if(Used[x]) return false;
    Used[x] = true;
    for(auto it: G[x]){
        if(B[it] == 0){
            A[x] = it; B[it] = x;
            return true;
        }
    }
    for(auto it: G[x]){
        if(Match(it)){
            A[x] = it; B[it] = x;
            return true;
        }
    }
    return false;
}

inline void Solve(const int &n){
    bool matching = true;
    int ans = 0;
    while(matching){
        matching = 0;
        memset(Used, 0, sizeof(Used));
        for(int i = 1; i <= n; i++){
            if(A[i] == 0 && Match(i)){
                matching = 1;
                ans++;
            }
        }
    }
    fout << ans << "\n";
}

int main(){
    int n, m, t, a, b;
    fin >> n >> m >> t;
    for(int i = 1; i <= t; i++){
        fin >> a >> b;
        G[a].push_back(b);
    }
    Solve(n);
    for(int i = 1; i <= n; i++){
        if(A[i]){
            fout << i << " " << A[i] << "\n";
        }
    }
    return 0;
}