Cod sursa(job #3238837)

Utilizator Andrei-27Arhire Andrei Andrei-27 Data 30 iulie 2024 22:29:33
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb
#include <bits/stdc++.h>
#define sqr(x) ((x) * (x))

using namespace std;
using ld = long double;

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

int n, k, m;
vector<vector<int>> g;
vector<int> mt, l;
vector<bool> used;



class InParser {
    vector<char> str;
    int ptr;
    ifstream fin;

    char getChar() {
        if (ptr == int(str.size())) {
            fin.read(str.data(), str.size());
            ptr = 0;
        }
        return str[ptr++];
    }

    template<class T>
    T getInt() {
        char chr = getChar();
        while (!isdigit(chr) && chr != '-')
            chr = getChar();
        int sgn = +1;
        if (chr == '-') {
            sgn = -1;
            chr = getChar();
        }
        T num = 0;
        while (isdigit(chr)) {
            num = num * 10 + chr - '0';
            chr = getChar();
        }
        return sgn * num;
    }

public:
    InParser(const char* name) : str(1e5), ptr(str.size()), fin(name) { }
    ~InParser() { fin.close(); }

    template<class T>
    friend InParser& operator>>(InParser& in, T& num) {
        num = in.getInt<T>();
        return in;
    }
};

class OutParser {
    vector<char> str;
    int ptr;
    ofstream fout;

    void putChar(char chr) {
        if (ptr == int(str.size())) {
            fout.write(str.data(), str.size());
            ptr = 0;
        }
        str[ptr++] = chr;
    }

    template<class T>
    void putInt(T num) {
        if (num < 0) {
            putChar('-');
            num *= -1;
        }
        if (num > 9)
            putInt(num / 10);
        putChar(num % 10 + '0');
    }

public:
    OutParser(const char* name) : str(1e5), ptr(0), fout(name) { }
    ~OutParser() { fout.write(str.data(), ptr); fout.close(); }

    template<class T>
    friend OutParser& operator<<(OutParser& out, const T num) {
        out.putInt(num);
        return out;
    }

    friend OutParser& operator<<(OutParser& out, const char chr) {
        out.putChar(chr);
        return out;
    }

    friend OutParser& operator<<(OutParser& out, const char* str) {
        for (int i = 0; str[i]; i++)
            out.putChar(str[i]);
        return out;
    }
};


bool try_kuhn(int v) {
    if (used[v])
        return false;
    used[v] = true;
    for (int to : g[v]) {
        if (mt[to] == -1 || try_kuhn(mt[to])) {
            l[v] = to;
            mt[to] = v;
            return true;
        }
    }
    return false;
}

int main() {

    InParser fin("cuplaj.in");
    OutParser fout("cuplaj.out");

    fin >> n >> k >> m;
    g.assign(n, vector<int>());
    while(m -- ){
        int x, y;
        fin >> x >> y;
        -- x, --y;
        g[x].emplace_back(y);
    }
    
    mt.assign(k, -1);

    int change = 1;
    l.assign(n, -1);

    while(change){
        change = 0;
        used.assign(n, false);
        for (int v = 0; v < n; ++v) {
            if (l[v] == -1){
                change |= try_kuhn(v);
            }
        }
    }



    vector<pair<int ,int>> ans;
    for (int i = 0; i < k; ++i) {
        if (mt[i] != -1)    {
            ans.emplace_back(mt[i] + 1, i + 1);
        }
    }
    sort(ans.begin(), ans.end());
    fout << ans.size() << '\n';
    for (auto it : ans){
        fout << it.first << ' ' << it.second << '\n';
    }
    return 0;
}