Cod sursa(job #3220181)

Utilizator Botnaru_VictorBotnaru Victor Botnaru_Victor Data 2 aprilie 2024 18:15:48
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.33 kb
#include <bits/stdc++.h>
using namespace std;

///----------TOGGLES
#define FIO
//#define LONGER
//#define TESTS
///---------

#ifdef LONGER
    #define int long long
#endif // LONGER

#ifdef FIO
    const string fname="cuplaj";
    ifstream in(fname+".in");
    ofstream out(fname+".out");
    #define cin in
    #define cout out
#endif // FIO

///--------

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

///-------STL++-----
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) x.size()
#define pb(x,y) x.push_back(y)
#define mp(x,y) make_pair(x,y)
#define fr(i,n) for(int i=1;i<=n;i++)
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pdd = pair<double, double>;
using veci = vector<int>;
using vecp = vector<pii>;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T> >;

///-----CODE GOES HERE-----

const int maxn = 10005;

int n,k,m;
int st[maxn], dr[maxn];


bool vis[maxn];
veci g[maxn];

bool doCupl(int x)
{
    vis[x]=1;
    for(auto e:g[x])
    {
        if(dr[e]==0)
        {
            dr[e]=x;
            st[x]=e;
            return 1;
        }
    }

    for(auto e:g[x])
    {
        if(!vis[dr[e]]&&doCupl(dr[e]))
        {
            dr[e]=x;
            st[x]=e;
            return 1;
        }
    }

    return 0;
}

void solve()
{
    {
        cin>>n>>k>>m;
        int a,b;
        for(int i=1;i<=m;i++)
        {
            cin>>a>>b;
            pb(g[a],b);
        }
    }

    int oldans=-1, ans=0;
    while(oldans!=ans)
    {
        oldans=ans;
        for(int i=1;i<=n;i++) vis[i]=0;
        for(int i=1;i<=n;i++)
        {
            if(st[i]==0&&doCupl(i)) ans++;
        }
        /*for(int i=1;i<=n;i++)
        {
            cout<<st[i]<<' ';
        }
        cout<<'\n';
        for(int i=1;i<=k;i++)
        {
            cout<<dr[i]<<' ';
        }
        cout<<'\n';*/
    }

    cout<<ans<<'\n';
    for(int i=1;i<=n;i++)
    {
        if(st[i]!=0)
        {
            cout<<i<<' '<<st[i]<<'\n';
        }
    }
    return;
}

///---MAIN FUNC-------

int32_t main()
{
    IOS;
    int t=1;
    #ifdef TESTS
        cin>>t;
    #endif // TESTS
    while(t--)
    {
        solve();
    }
    return 0;
}