Cod sursa(job #2564886)

Utilizator petrisorvmyVamanu Petru Gabriel petrisorvmy Data 2 martie 2020 10:51:07
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
#include <bits/stdc++.h>
#define FILE_NAME "cuplaj"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 10010
using namespace std;

ifstream f(FILE_NAME ".in");
ofstream g(FILE_NAME ".out");

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
typedef pair<ld,ld> pct;

const ll inf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;


void add(ll &a , ll b)
{
    a += b;
    a %= mod;
}

void sub(ll &a, ll b)
{
    a = (a - b + mod) % mod;
}

int st, dr, m, stanga[NMAX], dreapta[NMAX], a, b, cuplaje;
vector <int> G[NMAX];
bitset <NMAX> viz;

int DFS(int nod)
{
    if(viz[nod]) return 0;
    viz[nod] = 1;
    for(int i = 0; i < G[nod].size(); ++i)
    {
        int urm = G[nod][i];
        if( !dreapta[urm] || DFS(dreapta[urm]) )
        {
            stanga[nod] = urm;
            dreapta[urm] = nod;
            return 1;
        }
    }
    return 0;
}

int main()
{
    f >> st >> dr >> m;
    for(int i = 1; i <= m ; ++i)
    {
        f >> a >> b;
        G[a].push_back(b);
    }
    bool ok = 1;
    while(ok)
    {
        for(int i = 1; i <= st; ++i)
            viz[i] = 0;
        ok = 0;
        for(int i = 1; i <= st; ++i)
            if(!stanga[i] && DFS(i))
                ok = 1;
    }
    for(int i = 1; i <= st; ++i)
        if(stanga[i])
            cuplaje++;
    g << cuplaje << '\n';

    for(int i = 1; i <= st; ++i)
        if(stanga[i])
            g << i << ' ' << stanga[i] << '\n';
    f.close();
    g.close();
    return 0;
}