Cod sursa(job #2173937)

Utilizator TudorFinaruTudor Cristian Finaru TudorFinaru Data 16 martie 2018 09:45:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include<fstream>
#include<vector>
#include<cstring>
#include<bitset>

using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int n,m,e,match1[10003],match2[10003];

vector<int>v[10003];
bool viz[10003];

int match(int ind)
{
    if(viz[ind]) return 0;
    viz[ind]=1;
    vector<int>::iterator it;
    for(it=v[ind].begin();it!=v[ind].end();++it)
        if(match2[*it]==0)
        {
            match1[ind]=*it;
            match2[*it]=ind;
            return 1;
        }
    for(it=v[ind].begin();it!=v[ind].end();++it)
        if(match(match2[*it]))
        {
            match1[ind]=*it;
            match2[*it]=ind;
            return 1;
        }
    return 0;
}

int main()
{
    int i,x,y;
    f>>n>>m>>e;
    for(i=1;i<=e;i++)
    {
        f>>x>>y;
        v[x].push_back(y);
    }
    int ok=1;
    while(ok)
    {
        ok=0;
        memset(viz,0,sizeof viz);
        for(int i=1;i<=n;i++)
            if(!match1[i]) ok+=match(i);
    }
    int matches=0;
    for(int i=1;i<=n;i++)
        if(match1[i]) matches++;
    g<<matches<<'\n';
    for(i=1;i<=n;i++)
        if(match1[i]) g<<i<<' '<<match1[i]<<'\n';
    f.close();
    g.close();
    return 0;
}