Cod sursa(job #2841213)

Utilizator 100pCiornei Stefan 100p Data 29 ianuarie 2022 13:16:55
Problema Cuplaj maxim in graf bipartit Scor 18
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.53 kb
#include <bits/stdc++.h>
#define ull unsigned long long
#define FILES freopen("cuplaj.in","r",stdin);\
              freopen("cuplaj.out","w",stdout);
#define CMAX 1000000
#define fastio std::ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define mp make_pair
#define INF 999999999999999
#define mod 1000000007
#define ll long long
#define MAX 80000
#define SMAX 500000
#define pb push_back
#define add emplace_back
#define void inline void

using namespace std;
int n,m,e,matched[MAX+5],matched2[MAX+5];
bool ok[MAX+5];
vector<int> v[MAX+5];
bool cuplaj(int x)
{
    if(ok[x]) return 0;
    ok[x] = 1;
    for(auto i : v[x])
    {
        if(!matched[i])
        {
            matched[i] = x;
            matched2[x] = i;
            return 1;
        }
    }
    for(auto i : v[x])
    {
        if(cuplaj(matched[i]))
        {
            matched[i] = x;
            matched2[x] = i;
            return 1;
        }
    }
    return 0;
}
int main()
{
    fastio
    FILES
    cin >> n >> m >> e;
    for(int i = 1; i <= e; ++i)
    {
        int a,b;
        cin >> a >> b;
        v[a].add(b);
    }
    int tot = 0;
    while(1)
    {
        memset(ok,false,sizeof(ok));
        int gd = 0;
        for(int i = 1;i <= n; ++i){
            int l = cuplaj(i);
            gd |= l;
            tot += l;
        }
        if(!gd) break;
    }
    cout << tot << '\n';
    for(int i = 1;i <= n; ++i)
    {
        if(matched2[i]) cout << i << ' ' << matched2[i] << '\n';
    }
}