Cod sursa(job #755731)

Utilizator visanrVisan Radu visanr Data 6 iunie 2012 20:39:18
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;

#define pb push_back
#define nmax 10010

bool used[nmax];
int L[nmax], R[nmax], N, M, E, x, y;
vector<int> G[nmax];

int cuplaj();
int PairUp(int node);

int main()
{
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    int i;
    scanf("%i %i %i", &N, &M, &E);
    while(E--)
    {
              scanf("%i %i", &x, &y);
              G[x].pb(y);
    }
    printf("%i\n", cuplaj());
    for(i = 1; i <= N; i++)
          if(L[i])
                  printf("%i %i\n", i, L[i]);
    scanf("%i", &i);
    return 0;
}

int PairUp(int node)
{
    if(used[node]) return 0;
    used[node] = 1;
    vector<int> :: iterator it;
    for(it = G[node].begin(); it != G[node].end(); ++it)
    {
           if(R[*it] == 0 || PairUp(R[*it]))
           {
                     L[node] = *it;
                     R[*it] = node;
                     return 1;
           }
    }
    return 0;
}

int cuplaj()
{
    int ok = 1, counter = 0;
    while(ok)
    {
             memset(used, 0, sizeof(bool)*(N+1));
             ok = 0;
             for(int i = 1; i <= N; i++)
                   if(L[i] == 0)
                           if(PairUp(i))
                           {
                                        counter++;
                                        ok = 1;
                           }
    }
    return counter;
}