Cod sursa(job #2298309)

Utilizator SqueekDanielTodasca Daniel SqueekDaniel Data 7 decembrie 2018 23:05:38
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

#define MAXN 500005

int N, M, E;
std::vector <int> ADC[MAXN];

inline void AddEdge(int x, int y) {
    ADC[x].push_back(y);
}

int LPair[MAXN],
    RPair[MAXN];
bool Seen[MAXN];

bool PairUp(int Node) {
    if (Seen[Node]) return false;
    Seen[Node] = 1;

    for (auto Vecin:ADC[Node])
        if (!RPair[Vecin] || PairUp(RPair[Vecin])) {
            RPair[Vecin] = Node;
            LPair[Node] = Vecin;
            return true;
        }   return false;
}

void Match() {
    bool Check = 1;
    while (Check) {
        Check = 0;
        for (int i=1; i<=N; ++i)
            Seen[i] = 0;
        for (int i=1; i<=N; ++i)
            if (!LPair[i])
                Check |= PairUp(i);
    }
}


std::ifstream In("cuplaj.in");
std::ofstream Out("cuplaj.out");

void Citire() {
    In >> N >> M >> E;
    for (int i=0, x, y; i<E; ++i)
        In >> x >> y, AddEdge(x, y);
}

void Rezolvare() {
    Match();

    int Count = 0;
    for (int i=1; i<=N; ++i)
        if (LPair[i])
            ++ Count;
    Out << Count << '\n';

    for (int i=1; i<=N; ++i)
        if (LPair[i])
            Out << i << ' ' << LPair[i] << '\n';
}

int main()
{
    Citire();
    Rezolvare();

    return 0;
}