Cod sursa(job #1154676)

Utilizator VisuianMihaiMihai Visuian VisuianMihai Data 26 martie 2014 12:11:12
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");

int l[10001], r[10001], x, y, n, m, e, res;
bool vis[10001];
vector<int>g[10001];

inline bool Cuplare(const int& x)
{
    if(vis[x]) return 0;
    vis[x]=1;
    for(vector<int>::iterator it=g[x].begin(); it<g[x].end(); it++ )
    {
        if(!r[*it] || Cuplare(r[*it]))
        {
            r[*it]=x;
            l[x]=*it;
            return 1;

        }
    }
    return 0;
}

int main()
{
    fin>>n>>m>>e;
    while(e--)
    {
        fin>>x>>y;
        g[x].push_back(y);
    }
    bool ok=1;
    while(ok)
    {
        ok=0;
        memset(vis,0,sizeof(vis));
        for(int i = 1; i<= n; i++ )
            if(!l[i] && Cuplare(i))
            {
                res++;
                ok=1;
            }
    }
    fout<<res<<'\n';
    for(int i = 1; i<= n; i++ )
        if(l[i]) fout<<i<<' '<<l[i]<<'\n';

    fin.close();
    fout.close();
    return 0;
}