Cod sursa(job #2339592)

Utilizator cicero23catalin viorel cicero23 Data 9 februarie 2019 10:05:57
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <iostream>
#include <fstream>
#define Nmax 10001
#include <vector>
#include <cstring>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int st[Nmax],dr[Nmax];
int viz[Nmax],k;
vector <int> v[Nmax];
int cuplare(int nod)
{
    int i,vec;
    if(viz[nod]) return 0;
    viz[nod]=1;
    for(i=0;i<v[nod].size();++i)
    {
        vec=v[nod][i];
        if(!dr[vec])
        {
            dr[vec]=nod;
            st[nod]=vec;
            return 1;
        }
    }
    for(i=0;i<v[nod].size();++i)
    {
        vec=v[nod][i];
        if(cuplare(dr[vec]))
        {
            dr[vec]=nod;
            st[nod]=vec;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int n,m,mu,x,y;
    f>>n>>m>>mu;
    for(int i=1;i<=mu;++i)
    {
        f>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int ok=1;
    while(ok)
    {
        ok=0;
        memset(viz,0,sizeof(viz));
        for(int i=1;i<=n;i++)
            if(!st[i]) {ok|=cuplare(i);}
    }
    for(int i=1;i<=m;i++)
        if(st[i])k++;
    g<<k<<'\n';
    for(int i=1;i<=m;i++)
    if(st[i]){g<<st[i]<<" "<<i<<'\n';}
    return 0;
}