Cod sursa(job #2122625)

Utilizator nerelog25Radu Andrei Stefan nerelog25 Data 5 februarie 2018 12:42:59
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#define nmax 10001
#include <vector>
#include <string.h>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
vector <int> v[nmax];
int st[nmax],dr[nmax],viz[nmax];
bool pair_up(int x)
{
    if(viz[x]) return 0;
    viz[x]=1;
    for(int i=0; i<v[x].size(); ++i)
        if(dr[v[x][i]]==0)
        {
            st[x]=v[x][i];
            dr[v[x][i]]=x;
            return 1;
        }
    for(int i=0; i<v[x].size(); ++i)
        if(pair_up(dr[v[x][i]]))
        {
            st[x]=v[x][i];
            dr[v[x][i]]=x;
            return 1;
        }
    return 0;
}
int main()
{
    int n,m,e,i,x,y,ok,nr=0;
    f>>n>>m>>e;
    for(i=1; i<=e; i++)
    {
        f>>x>>y;
        v[x].push_back(y);
    }
    ok=1;
    while(ok)
    {
        memset(viz,0,sizeof(viz));
        ok=0;
        for(i=1; i<=n; ++i)
            if(!st[i]) ok|=pair_up(i);
    }
    for(i=1;i<=n;i++)
        if(st[i])
           nr++;
    g<<nr<<'\n';
    for(i=1;i<=n;i++)
        if(st[i])
        g<<i<<" "<<st[i]<<'\n';
    return 0;
}