Cod sursa(job #2854629)

Utilizator levladiatorDragutoiu Vlad-Ioan levladiator Data 21 februarie 2022 15:51:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
#define ull unsigned long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define EPSILON 0.000001

using namespace std;

const ll NMAX = 1e5+5, INF = 1e18, MOD = 1e9 + 7, MMAX = 1e2 + 5, inf = INT_MAX;

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

vector<int> G[NMAX];
int N, M, E,
    L[NMAX], R[NMAX],
    maxMatch;
bool viz[NMAX];


void citire()
{
    int x, y;
    f >> N >> M >> E;
    while(E--)
    {
        f >> x >> y;
        G[x].push_back(y);
    }
}

bool DFS(int x)
{
    if(viz[x]) return 0;
    viz[x] = 1;
    for(auto &y : G[x])
        if(!R[y] || DFS(R[y]))
        {
            L[x] = y;
            R[y] = x;
            return 1;
        }
    return 0;
}

void HK() ///Hopcroft-Karp
{
    bool wasChanged = 1;
    int i;
    while(wasChanged)
    {
        wasChanged = 0;
        for(i = 1; i <= N; ++i)
            viz[i] = 0;
        for(i = 1; i <= N; ++i)
        {
            if(!L[i] && DFS(i))
            {
                wasChanged = 1;
                maxMatch++;
            }
        }
    }
}

int main()
{
    cin.tie ( 0 )->sync_with_stdio ( 0 );
    cin.tie ( NULL );
    cout.tie ( NULL );

    citire();
    HK();
    g << maxMatch << '\n';
    for(int i = 1; i <= N; i++)
    {
        if(L[i]) g << i << ' ' << L[i] << '\n';
    }
    f.close();
    g.close();

    return 0;
}