Cod sursa(job #2187557)

Utilizator ajeccAjechiloae Eugen ajecc Data 26 martie 2018 16:48:51
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.11 kb
#include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define index INDEX
#ifdef _WIN32
#include <windows.h>
#define print(x) PRINT(x, #x)
template<typename T> inline const void PRINT(T VARIABLE, string NAME)
{
#ifndef ONLINE_JUDGE /// ONLINE_JUDGE IS DEFINED ON CODEFORCES
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, 10);
    cerr << NAME << " = " << VARIABLE;
    SetConsoleTextAttribute(hConsole, 7);
    cerr << '\n';
#endif
}
#else
#define print(x) 0
#endif
typedef long long ll;
typedef unsigned long long ull;
const ll INFLL = 2 * (ll)1e18 + 100;
const int INFINT = 2 * (int)1e9 + 100;
const double PI = atan(1) * 4;
const double EPS = 1e-9;
const int SEED = 137;

const int MOD = 1e9 + 7; /// careful here (7 or 9, 66.. etc)25000033
const int NMAX = 1e4 + 5;

int n, m, e;
V graf[NMAX];

int l_match[NMAX], r_match[NMAX];

bool trecut[NMAX];
inline bool pair_up(int nod)
{
    if(trecut[nod]) return 0;
    trecut[nod] = 1;
    for(auto i: graf[nod]) if(!r_match[i])
    {
        l_match[nod] = i;
        r_match[i] = nod;
        return 1;
    }

    for(auto i: graf[nod]) if(pair_up(r_match[i]))
    {
        l_match[nod] = i;
        r_match[i] = nod;
        return 1;
    }

    return 0;
}

int main()
{
    ifstream cin("cuplaj.in");
    ofstream cout("cuplaj.out");
    cin >> n >> m >> e;
    for1(i, e)
    {
        int a, b;
        cin >> a >> b;
        graf[a].pb(b);
    }

    for(;;)
    {
        memset(trecut, 0, sizeof(trecut));
        bool ok = 0;
        for1(i, n) if(!l_match[i]) ok |= pair_up(i);
        if(!ok) break;
    }

    int sol = 0;
    for1(i, n) if(l_match[i]) sol++;
    cout << sol << '\n';
    for1(i, n) if(l_match[i]) cout << i << ' ' << l_match[i] << '\n';

    return 0;
}