Cod sursa(job #2736638)

Utilizator dimi999Dimitriu Andrei dimi999 Data 3 aprilie 2021 18:34:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int L, R, M;

vector <int> st[10005], dr[10005];

int lleft[10005], rright[10005];

bool vis[10005];

bool pairup(int node)
{
    if(vis[node] == true)
        return false;

    vis[node] = true;

    for(int i = 0; i < st[node].size(); i++)
        if(rright[st[node][i]] == 0)
    {
        rright[st[node][i]] = node;
        lleft[node] = st[node][i];
        return true;
    }

    for(int i = 0; i < st[node].size(); i++)
        if(pairup(rright[st[node][i]]) == true)
    {
        rright[st[node][i]] = node;
        lleft[node] = st[node][i];
        return true;
    }

    return false;
}

int main()
{
    fin >> L >> R >> M;

    for(int i = 1; i <= M; i++)
    {
        int x, y;

        fin >> x >> y;

        st[x].push_back(y);
        dr[y].push_back(x);
    }

    bool ok = true;
    int ct = 0;

    while(ok)
    {
        memset(vis + 1, 0, L);
        ok = false;

        for(int i = 1; i <= L; i++)
            if(lleft[i] == 0)
                ok |= pairup(i);
    }

    for(int i = 1; i <= L; i++)
        if(lleft[i] != 0)
            ct++;

    fout << ct << '\n';

    for(int i = 1; i <= L; i++)
        if(lleft[i] != 0)
            fout << i << " " << lleft[i] << '\n';

    return 0;
}