Cod sursa(job #1923119)

Utilizator tudor_bonifateTudor Bonifate tudor_bonifate Data 10 martie 2017 20:49:18
Problema Cuplaj maxim in graf bipartit Scor 4
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
vector <int> G[20005];
int n,m,e,x,y,i,U[20005],dr[20005],st[20005],sol,nr;
bool cupleaza(int node)
{
    int i;
    if (U[node]) return 0;
    U[node]=1;
    for (i=0; i<G[node].size(); i++)
    {
        int crt=G[node][i];
        if (!dr[crt] || cupleaza(dr[crt]))
        {
            st[node]=crt;
            dr[crt]=node;
            return 1;
        }
    }
}
void cuplaj()
{
    int i;
    nr=1;
    while (nr>0)
    {
        nr=0;
        memset(U,0,sizeof(U));
        for (i=1; i<=n; i++)
        {
            if (st[i]) continue;
            if (cupleaza(i)) nr++;
        }
    }
}
int main()
{
    freopen("cuplaj.in","r",stdin);
    freopen("cuplaj.out","w",stdout);
    scanf("%d %d %d",&n,&m,&e);
    for (i=1; i<=e; i++)
    {
        scanf("%d %d",&x,&y);
        G[x].push_back(y+n);
        G[y+n].push_back(x);
    }
    cuplaj();
    sol=0;
    for (i=1; i<=n; i++) if (st[i]) sol++;
    printf("%d\n",sol);
    for (i=1; i<=n; i++) if (st[i]) printf("%d %d\n",i,st[i]-n);
    return 0;
}