Cod sursa(job #1867688)

Utilizator savulescustefanSavulescu Stefan savulescustefan Data 4 februarie 2017 11:43:02
Problema Cuplaj maxim in graf bipartit Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.97 kb
#include <cstdio>
#include <vector>
using namespace std;
vector <int> v[10004],v1[10004],s;
int n,m,e,i,x,y,nr1,nr[10004],a[10004],b[10004],k,j,h;
bool ok,c[10004];
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);
        v[x].push_back(y);
        v1[y].push_back(x);
        nr[x]++;
    }
    nr1=0;
    for (i=1;i<=n;i++)
    {
        ok=false;
        k=v[i].size();
        for (j=0;j<=(k-1);j++)
        {
            if (a[v[i][j]]==0)
            {
                a[v[i][j]]=1;
                b[v[i][j]]=i;
                ok=true;
                nr1++;
                for (h=0;h<=(v1[v[i][j]].size()-1);h++)
                    nr[v1[v[i][j]][h]]--;
                break;
            }
        }
        if (ok==false)
            s.push_back(i);
    }
    h=s.size();
    ok=true;
    while (ok==true && k>=1)
    {
        ok=false;
        for (i=0;i<=(h-1);i++)
            for (j=0;j<=(v[s[i]].size()-1);j++)
            {
                if (nr[b[v[s[i]][j]]]!=0)
                {
                    x=b[v[s[i]][j]];
                    for (k=0;k<=v[x].size();k++)
                    {
                        if (a[v[x][k]]==0)
                        {
                            a[v[x][k]]=1;
                            b[v[x][k]]=x;
                            b[v[s[i]][j]]=s[i];
                            for (h=0;h<=(v1[v[x][k]].size()-1);h++)
                                nr[v1[v[x][k]][h]]--;
                            break;
                        }
                    }
                    ok=true;
                    nr1++;
                    h++;
                    break;
                }
            }
    }
    printf ("%d\n", nr1);
    for (i=1;i<=m;i++)
    {
        if (b[i]!=0)
            printf ("%d %d\n", b[i], i);
    }
    return 0;
}