Cod sursa(job #2725702)

Utilizator Adrian.TrillMititean Adrian Adrian.Trill Data 19 martie 2021 15:44:45
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.77 kb
#include <stdio.h>
#include <vector>
#include <bitset>
#include <algorithm>
#include <string.h>
 
using namespace std;
 
const int MAXN = 10001;
 
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
typedef unsigned int uuint;
 
Graph G;
uuint m, n, e, sol;
uuint pereche[MAXN], cuplaj[MAXN];
bitset<MAXN> used;
char s[9];
 
bool pairup(uuint node)
{
    if(used[node])
        return false;
    used[node] = true;
    for(It it = G[node].begin(), fin = G[node].end(); it != fin ; ++ it)
        if(!pereche[*it])
        {
            cuplaj[node] = *it;
            pereche[*it] = node;
            return true;
        }
    for(It it = G[node].begin(), fin = G[node].end(); it != fin ; ++ it)
        if(pairup(pereche[*it]))
        {
            cuplaj[node] = *it;
            pereche[*it] = node;
            return true;
        }
    return false;
}
const void solve(void)
{
    sol = 0;
    scanf("%d %d %d", &m, &n, &e);
    memset(pereche, 0, sizeof(pereche));
    memset(cuplaj, 0, sizeof(cuplaj));
    for(uuint i = 1 ; i <= m ; ++ i)
        G[i].clear();
    for(uuint i = 1; i <= e ; ++ i)
    {
        uuint x, y;
        scanf("%d %d", &x, &y);
        G[x].push_back(y);
    }
    bool ok;
    do{
        ok = false;
        used.reset();
        for(uuint i = 1 ; i <= m ; ++ i)
            if(!cuplaj[i])
                if(pairup(i))
                {
                    ++ sol;
                    ok = true;
                }
 
    }while(ok);
    printf("%d\n", sol);
}
 
int main()
{
    freopen("java.in", "r", stdin);
    freopen("java.out", "w", stdout);
    uuint t;
    for( scanf("%d", &t) ; t ; -- t)
        solve();
 
    fclose(stdin);
    fclose(stdout);
    return 0;
}