Cod sursa(job #2695422)

Utilizator vladboss2323Ciorica Vlad vladboss2323 Data 12 ianuarie 2021 22:17:25
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");

const int N = 1e4 + 5;

int n, m, e, Left[N], Right[N], nr_pair_matched;

vector<int> a[N];
bool viz[N];

bool dfs(int node)
{
    if (viz[node])
    {
        return false;
    }
    viz[node] = 1;
    for (int nei : a[node])
    {
        if (!Right[nei] || dfs(Right[nei]))
        {
            Left[node] = nei;
            Right[nei] = node;
            return true;
        }
    }

    return false;
}

int main()
{

    std::cin >> n >> m >> e;

    for(int i = 1 ; i <= e ; i++)
    {
        int x, y;
        std::cin >> x >> y;
        a[x].push_back(y);
    }

    bool found_pair = true;
    while (found_pair)
    {
        found_pair = false;
        for (int i = 1; i <= n; ++i)
        {
            viz[i] = 0;
        }

        for (int i = 1; i <= n; ++i)
        {
            if (!Left[i] && dfs(i))
            {
                found_pair = true;
                nr_pair_matched++;
            }
        }
    }

    std::cout << nr_pair_matched << '\n';
    /*
    for (int i = 1; i <= n; ++i)
    {
        if (Left[i])
        {
            std::cout << i << ' ' << Left[i] << '\n';
        }
    }
    */

    return 0;
}