Cod sursa(job #1156543)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 27 martie 2014 19:21:39
Problema Felinare Scor 0
Compilator cpp Status done
Runda oni_11_12_6 Marime 1.09 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <bitset>

#define Nmax 9000

using namespace std;
int N,M,L[Nmax],R[Nmax],LS[Nmax],RS[Nmax];
vector<int> G[Nmax];
bitset<Nmax> used;

void read()
{
    scanf("%d%d",&N,&M);
    int a,b;
    for(int i = 1; i <= M; ++i)
    {
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
    }
}

inline bool cuplaj(int k)
{
    if(used[k]) return false;
    used[k] = 1;
    for(vector<int>::iterator it = G[k].begin(); it != G[k].end(); ++it)
        if(!R[*it] || cuplaj(L[R[k]]))
        {
            L[k] = *it;
            R[*it] = k;
            return true;
        }
    return false;
}

int main()
{
    freopen("felinare.in","r",stdin);
    freopen("felinare.out","w",stdout);

    read();
    int ok = 1,nr_cuplate = 0;
    while(ok)
    {
        ok = 0;
        used = 0;
        for(int i = 1; i <= N; ++i)
            if(!L[i] && cuplaj(i))
            {
                ok = 1;
                nr_cuplate++;
            }
    }
    printf("%d\n",2*N-nr_cuplate);
    return 0;
}