Cod sursa(job #2415687)

Utilizator HaesteinnSabau Florin Vlad Haesteinn Data 26 aprilie 2019 14:00:55
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
#define NMAX 10005
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

int n,m,e;
vector<int> adj[NMAX];

int st[NMAX],dr[NMAX];
int vis[NMAX];

bool pairup(int x,int step)
{
    if(vis[x]==step) return 0;
    vis[x]=step;
    for(auto y:adj[x])
        if(dr[y]==0)
        {
            dr[y]=x;
            st[x]=y;
            return 1;
        }

    for(auto y:adj[x])
        if(pairup(dr[y],step))
        {
            dr[y]=x;
            st[x]=y;
            return 1;
        }
    return 0;
}

int main()
{
    fin>>n>>m>>e;
    for(int i=0;i<e;i++)
    {
        int l,r;
        fin>>l>>r;
        adj[l].push_back(r);
    }
    bool ok;
    int step=1;
    do
    {
        ok=0;
        for(int i=1;i<=n;i++)
            if(st[i]==0)
                ok|=pairup(i,step);
        step++;
    } while(ok);
    vector<pair<int,int> > match;
    for(int i=1;i<=n;i++)
        if(st[i])
    match.push_back({i,st[i]});
    fout<<match.size()<<'\n';
    for(auto x:match)
        fout<<x.first<<' '<<x.second<<'\n';

    return 0;
}