Cod sursa(job #3144162)

Utilizator AdiFBubuBubuianu Adrian AdiFBubu Data 5 august 2023 14:39:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <bits/stdc++.h>

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

int N, M, E, x, y, contor;
int l[10005], r[10005], u[10005];
vector <int> G[10005];

int pairup(int n)
{
    if(u[n]) return 0;
    u[n] = 1;
    for(vector <int> ::iterator i = G[n].begin(); i != G[n].end(); i ++)
        if(r[*i] == 0)
        {
            l[n] = *i;
            r[*i] = n;
            return 1;
        }
    for(vector <int > ::iterator i = G[n].begin(); i != G[n].end(); i ++)
        if(pairup(r[*i]) != 0)
        {
            l[n] = *i;
            r[*i] = n;
            return 1;
        }
    return 0;
}

int main()
{
    f >> N >> M >> E;
    for(int i = 1; i <= E; i ++)
    {
        f >> x >> y;
        G[x].push_back(y);
    }
    int ok = 1;
    while(ok)
    {
        ok = 0;
        for(int i = 1; i <= N; i ++)
            u[i] = 0;
        for(int i = 1; i <= N; i ++)
            if(l[i] == 0)
                ok = ok | pairup(i);
    }
    for(int i = 1; i <= N; i ++)
        if(l[i] != 0)
            contor ++;
    g << contor << '\n';
    for(int i = 1; i <= N; i ++)
        if(l[i] != 0)
            g << i << " " << l[i] << '\n';
    return 0;
}