Cod sursa(job #3268944)

Utilizator solicasolica solica solica Data 18 ianuarie 2025 09:47:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
/*
  ____   ___  _     ___ ____    _
 / ___| / _ \| |   |_ _/ ___|  / \
 \___ \| | | | |    | | |     / _ \
  ___) | |_| | |___ | | |___ / ___ \
 |____/ \___/|_____|___\____/_/   \_\

*/
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define int long long int
#define pii pair<int,int>

const int NMAX = 2e4+9;

const int MOD = 1e9+7;

int binpow(int n, int k)
{
    if (k==0)
    {
        return 1;
    }
    int x=binpow(n,k/2)%MOD;
    if (k%2==0)
    {
        return x*x%MOD;
    }
    else
    {
        return x*x%MOD*n%MOD;
    }
}

ifstream fin ("cuplaj.in");
ofstream fout ("cuplaj.out");

#define cin fin
#define cout fout

int n,m,e,i,j,a,b;

vector<int>g[NMAX]; 

bool used[NMAX];

int l[NMAX],mt[NMAX];

bool match (int node)
{
    if (used[node])
    {
        return 0;
    }
    used[node]=1;
    for (auto it : g[node])
    {
        if (mt[it]==-1 || match(mt[it]))
        {
            l[node]=it;
            mt[it]=node;
            return 1;
        }
    }
    return 0;
}

void run_case ()
{
    cin>>n>>m>>e;
    for (i=1; i<=e; i++)
    {
        cin>>a>>b;
        g[a].pb(b);
    }
    for (i=1; i<=m; i++)
    {
        mt[i]=-1;
    }
    for (i=1; i<=n; i++)
    {
        l[i]=-1;
    }
    bool change=1;
    while (change)
    {
        change=0;
        for (i=1; i<=n; i++)
        {
            used[i]=0;
        }
        for (i=1; i<=n; i++)
        {
            if (l[i]==-1)
            {
                change |= match(i);
            }
        }
    }
    vector<pii>answer;
    for (i=1; i<=m; i++)
    {
        if (mt[i]!=-1)
        {
            answer.push_back({mt[i],i});
        }
    }
    sort(answer.begin(),answer.end());
    cout<<answer.size()<<'\n';
    for (auto it : answer)
    {
        cout<<it.first<<' '<<it.second<<'\n';
    }
}

signed main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL),cout.tie (NULL);
    int teste;
    teste=1;
    while (teste--)
    {
        run_case();
    }
}