Cod sursa(job #2961055)

Utilizator HAUBAUHAULICA TUDOR HAUBAU Data 5 ianuarie 2023 17:35:04
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include  <vector>
#include  <string>
#include  <algorithm>
#include <fstream>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");


    int N,M,E;
    vector<int> G[100001];
    int viz[100001];
    int match[100001];


 bool pairup(int nod)
    {

        viz[nod]=1;
    for(int i=0;i<G[nod].size();i++)
    { int vecin=G[nod][i];
        if(!match[vecin] || (!viz[match[vecin]] && pairup(match[vecin])))
        {   match[vecin]=nod;
            match[nod]=vecin;
            return true;
        }


    }
        return false;
    }

int main() {
    int x,y,z;
    fin>>N>>M>>E;
    for(int i=1;i<=E;i++)
    {  fin>>x>>y;
        y += N;
        G[x].push_back(y);
        G[y].push_back(x);
    }
     int nr=0;
     bool ok = true;
     while(ok)
     {
            ok = false;
            for(int i=1;i<=N+M;i++)
            {
                if(!match[i] && pairup(i))
                {
                    ok = true;
                    nr++;
                }
            }
            for(int i=1;i<=N+M;i++)
            {
                viz[i] = 0;
            }


     }

    fout<<nr<<endl;
    for(int i=1;i<=N;i++)
        if(match[i])
            fout<<i<<" "<<match[i]-N<<endl;


    return 0;
}