Cod sursa(job #2679691)

Utilizator robert.barbu27robert barbu robert.barbu27 Data 1 decembrie 2020 11:47:03
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.41 kb
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define nmax 100005
#define inf 1000000000
using namespace std;

ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int N,M,E;
vector<int> adj[10005];
int l[10005],r[10005],cnt;
int viz[10005];
bool cuplaj(const int node)
{
    if(viz[node]==cnt)
    {
        return false;
    }
    viz[node]=cnt;
    //cout<<node<<" "<<cnt<<'\n';
    for(auto x:adj[node])
    {
        if(l[x]==0)
        {
            r[node]=x;
            l[x]=node;
            return true;
        }
    }
    for(auto x:adj[node])
    {
        if(cuplaj(l[x]))
        {
            r[node]=x;
            l[x]=node;
            return true;
        }
    }
    return false;
}
int main()
{

    f>>N>>M>>E;
    while(E--)
    {
        int x,y;
        f>>x>>y;
        adj[x].pb(y);
    }
    bool gasit=1;
    while(gasit==1)
    {
        gasit=0;
        cnt++;
        for(int i=1;i<=N;i++)
        {
            if(r[i]==0)
            {
                gasit=gasit|cuplaj(i);

            }
        }
       // cout<<1;
    }
    int rez=0;
    for(int i=1;i<=N;i++)
    {
        if(r[i])
        {
            rez++;
        }
    }
    g<<rez<<'\n';
    for(int i=1;i<=N;i++)
    {
        if(r[i])
        {
            g<<i<<" "<<r[i]<<'\n';

        }
    }








}