Cod sursa(job #3302730)

Utilizator FlaviuuuFlaviu Flaviuuu Data 10 iulie 2025 13:34:28
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.48 kb
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
#define ll long long
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
//chat sunt locked in???
vector<ll> a[10005];
vector<ll> b[10005];
bool viz[10005];
ll cupl[10005];
ll nxt[10005];
bool dfs(ll nod)
{
    if(viz[nod] == 1)
        return 0;
    viz[nod] = 1;
    for(int i = 0; i < a[nod].size(); i++)
        if(cupl[a[nod][i]] == 0)
            {cupl[a[nod][i]] = nod; nxt[nod] = a[nod][i]; return 1;}
    for(int i = 0; i < a[nod].size(); i++)
        if(dfs(cupl[a[nod][i]]))
            {cupl[a[nod][i]] = nod; nxt[nod] = a[nod][i]; return 1;}
    return 0;
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n, m, e;
    cin>>n>>m>>e;
    for(int i = 1; i <= e; i++)
    {
        ll x, y;
        cin>>x>>y;
        a[x].push_back(y);
        b[y].push_back(x);
    }
    bool gasit = 1; 
    while(gasit == 1)
    {
        gasit = 0;
        for(int i = 1; i <= n; i++)
            viz[i] = 0;
        for(int i = 1; i <= n; i++)
            if(!nxt[i])
                gasit |= dfs(i);
    }
    ll ans = 0;
    for(int i = 1; i <= m; i++)
        ans += (cupl[i] != 0);
    cout<<ans<<'\n';
    for(int i = 1; i <= m; i++)
        if(cupl[i])
            cout<<cupl[i]<<" "<<i<<'\n';
    return 0;
}