Cod sursa(job #2357486)

Utilizator Daria09Florea Daria Daria09 Data 27 februarie 2019 14:28:13
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>
#define dim 10005
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int Left[dim],Right[dim],cuplaj;
bool viz[dim];
vector <int> graph[dim];
int n,m;
void Read()
{
    int edge;
    f>>n>>m>>edge;
    for(int i=1; i<=edge; ++i)
    {
        int x,y;
        f>>x>>y;
        graph[x].push_back(y);
    }
}
bool Match(int node)
{
    if(viz[node])return false;
    viz[node]=1;
    for(int i=0; i<graph[node].size(); ++i)
    {
        int son=graph[node][i];
        if(!Right[son])
        {
            Right[son]=node;
            Left[node]=son;
            ++cuplaj;
            return true;
        }
    }
    for(int i=0; i<graph[node].size(); ++i)
    {
        int son=graph[node][i];
        if(Match(Right[son]))
        {
            Right[son]=node;
            Left[node]=son;
            return true;
        }
    }
    return false;
}
void Cuplaj()
{
    bool okay;
    do
    {
        for(int i=1; i<=n; ++i)
            viz[i]=0;
        okay=false;
        for(int i=1; i<=n; ++i)
            if(!Left[i])
                okay|=Match(i);
    }
    while(okay);
}
void Write()
{
    g<<cuplaj<<'\n';
    for(int i=1; i<=n; ++i)
        if(Left[i])
            g<<i<<" "<<Left[i]<<'\n';
}
int main()
{
    Read();
    Cuplaj();
    Write();
    return 0;
}