Cod sursa(job #3042206)

Utilizator alexddAlexandru Dima alexdd Data 4 aprilie 2023 19:03:27
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n,m,e;
vector<int> con[10005];
int tori[10005];
int tole[10005];
bool vis[10005];
bool change=0;
bool cupleaza(int x)
{
    if(vis[x])
        return 0;
    vis[x]=1;
    for(auto adj:con[x])
    {
        if(tole[adj]==0)
        {
            tori[x]=adj;
            tole[adj]=x;
            change=1;
            return 1;
        }
    }
    for(auto adj:con[x])
    {
        if(cupleaza(tole[adj]))
        {
            tori[x]=adj;
            tole[adj]=x;
            change=1;
            return 1;
        }
    }
    return 0;
}
void cuplaj()
{
    while(1)
    {
        change=0;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            if(tori[i]==0)
                cupleaza(i);
        if(change==0)
            break;
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(tori[i]!=0)
            cnt++;
    fout<<cnt<<"\n";
    for(int i=1;i<=n;i++)
        if(tori[i]!=0)
            fout<<i<<" "<<tori[i]<<"\n";
}
signed main()
{
    fin>>n>>m>>e;
    int x,y;
    for(int i=0;i<e;i++)
    {
        fin>>x>>y;
        con[x].push_back(y);
    }
    cuplaj();
    return 0;
}