Cod sursa(job #1156652)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 27 martie 2014 20:28:04
Problema Felinare Scor 40
Compilator cpp Status done
Runda oni_11_12_6 Marime 1.3 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(R[*it]))
        {
            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);
    for(int i = 1; i <= N; ++i)
    {
        LS[i] = 1;
        RS[i] = 2;
        if(L[i] != 0)
            LS[i] = 0;
        if(R[i] == i)
            RS[i] = 0;
        printf("%d\n",LS[i] + RS[i]);
    }
    return 0;
}