Cod sursa(job #3226329)

Utilizator matei__bBenchea Matei matei__b Data 20 aprilie 2024 23:03:58
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>
#define ll long long
#define mod 666013
#define pb push_back
#define mp make_pair
#define dim 100005
using namespace std;

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

int n1,n2,m;
vector<int> g[dim];

bool used[dim];
int st[dim];
int dr[dim];

bool pot(int nod)
{
    if(used[nod])
        return 0;

    used[nod]=1;

    for(auto it:g[nod])
    {
        if(dr[it]==0 || (dr[it]!=0 && pot(dr[it])))
        {
            dr[it]=nod;
            st[nod]=it;
            return 1;
        }
    }

    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin >> n1 >> n2 >> m;

    for(int i=1; i<=m; i++)
    {
        int x,y;

        fin >> x >> y;

        g[x].pb(y);
    }

    while(1)
    {
        bool ok=0;

        for(int i=1; i<=n1; i++)
            used[i]=0;

        for(int i=1; i<=n1; i++)
        {
            if(!st[i])
                ok|=pot(i);
        }

        if(!ok)
            break;
    }

    int cnt=0;

    for(int i=1; i<=n1; i++)
        cnt+=(st[i]>0);

    fout << cnt << "\n";

    for(int i=1; i<=n1; i++)
    {
        if(st[i])
            fout << i << " " << st[i] << "\n";
    }

    return 0;
}