Cod sursa(job #2848085)

Utilizator DesertChuStefan Andrei DesertChu Data 12 februarie 2022 09:48:30
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n,m,x,y,nr=0,ok=1,e;
int st[10105],dr[10105],viz[10105];
vector <int> v[nmax];
bool cupl(int nod)
{
    int i,vc;
    if(viz[nod]) return 0;
    viz[nod]=1;
    for(i=0; i<v[nod].size(); i++)
    {
        vc=v[nod][i];
        if(!dr[vc] || cupl(dr[vc]))
        {
            dr[vc]=nod;
            st[nod]=vc;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int i;
    f>>n>>m>>e;
    for(i=1; i<=e; i++)
    {
        f>>x>>y;
        v[x].push_back(y);
    }

    while(ok)
    {
        memset(viz,0,sizeof(viz));
        ok=0;
        for(i=1; i<=n; i++)
            if(!st[i] && cupl(i)) ok=1;
    }
    for(i=1; i<=n; i++)
        if(st[i])
            nr++;
    g<<nr<<'\n';
    for(i=1; i<=n; i++)
        if(st[i])
            g<<i<<' '<<st[i]<<'\n';
    return 0;
}