Cod sursa(job #3304807)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 27 iulie 2025 15:43:12
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int NMAX = 10001;
int n, m, e, pereche[NMAX], ans = 0;
bool vis[NMAX], cuplat[NMAX];
vector<int> adj[NMAX];

bool dfs(int node)
{
    if(vis[node])
        return false;
    vis[node] = true;
    for(int next : adj[node])
    {
        if(!pereche[next] || dfs(pereche[next]) == true)
        {
            pereche[next] = node;
            cuplat[node] = true;
            return true;
        }
    }
    return false;
}

int main()
{
    fin >> n >> m >> e;
    while(e--)
    {
        int u, v;
        fin >> u >> v;
        adj[u].push_back(v);
    }

    while(true)
    {
        bool ok = true;
        memset(vis, false, sizeof(vis));
        for(int i = 1; i <= n; i++)
            if(!cuplat[i] && dfs(i) == true)
                ok = false;
        if(ok == true)
            break;
    }
    for(int i = 1; i <= m; i++)
        ans += (pereche[i] != 0);
    fout << ans << "\n";
    for(int i = 1; i <= m; i++)
        if(pereche[i] != 0)
            fout << pereche[i] << " " << i << "\n";

    fin.close();
    fout.close();
    return 0;
}