Cod sursa(job #2739156)

Utilizator razvanradulescuRadulescu Razvan razvanradulescu Data 6 aprilie 2021 22:57:30
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
#define MAX_SIZE 100005
#define INF 0x3F3F3F3F
#define DUMMY 0
using namespace std;

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

vector<int> graph[MAX_SIZE];
int pairU[MAX_SIZE], pairV[MAX_SIZE];
bool viz[MAX_SIZE];
int n, m, e;

void read()
{
    int u, v;
    f>>n>>m>>e;
    for(int i = 0; i<e; i++)
    {
        f>>u>>v;
        graph[u].push_back(v);
    }
}

bool dfs(int u)
{
    viz[u] = true;
    for(auto &v : graph[u])
    {
        if(pairV[v] == 0 || (!viz[pairV[v]] && dfs(pairV[v])))
        {
            pairV[v] = u;
            pairU[u] = v;
            return true;
        }
    }
    return false;
}

void solve()
{
    int sol = 0;
    bool changed = true;
    while(changed)
    {
        changed = false;
        memset(viz, 0, sizeof(viz));
        for(int u = 1; u <= n; u++)
            if(pairU[u] == 0 && !viz[u] && dfs(u))
            {
                sol++;
                changed = true;
            }
    }
    g<<sol<<"\n";
}

void print()
{
    for(int u = 1; u<=n; u++)
    {
        if(pairU[u] != DUMMY)
        {
            g<<u<<" "<<pairU[u]<<"\n";
        }
    }
}

int main()
{
    read();
    solve();
    print();
    return 0;
}