Cod sursa(job #1779475)

Utilizator CiurezAndreiCiurez Marius-Andrei CiurezAndrei Data 15 octombrie 2016 13:07:15
Problema Felinare Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.95 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("felinare.in");
ofstream fout("felinare.out");

int supportLeft[10000], supportRight[10000], stanga[10000], dreapta[10000];
int N, M, a, b, cardCuplaj;
bool ok = true;
bitset<10000>v;
vector<int>L[10000];

inline bool cuplaj(const int &node)
{
    v[node] = 1;

    for(int i = 0; i < L[node].size(); i ++)
    {
        if(stanga[ L[node][i] ] == 0)
        {
            stanga[ L[node][i] ] = node;
            dreapta[node] = L[node][i];
            supportLeft[node] = 1;
            return true;
        }
    }

    for(int i = 0; i < L[node].size(); i ++)
    {
        if(v[ stanga[ L[node][i] ] ] == 0 && cuplaj( stanga[ L[node][i] ]) == true )
        {
            stanga[ L[node][i] ] = node;
            dreapta[node] = L[node][i];
            supportLeft[node] = 1;
            return true;
        }
    }
    return false;
}

inline void computeSupport(const int &node)
{
    for(int i = 0; i < L[node].size(); i ++)
    {
        if(supportRight[ L[node][i] ] == 0)
        {
            supportRight[ L[node][i] ] = 1;
            supportLeft [ stanga[ L[node][i] ] ] = 0;

            computeSupport( stanga[ L[node][i] ] );

        }
    }
}

int main()
{
    fin >> N >> M;

    for(int i = 1; i <= M; i ++)
    {
        fin >> a >> b;
        L[a].push_back(b);
    }

    while(ok)
    {
        ok = false;
        v.reset();

        for(int i = 1; i <= N; i ++)
        {
            if(dreapta[i] == 0 && cuplaj(i) == true)
            {
                cardCuplaj ++;
                ok = true;
            }
        }
    }

    for(int i = 1; i <= N; i ++)
    {
        if(supportLeft[i] == 0)
            computeSupport(i);
    }

    fout << 2 * N - cardCuplaj << '\n';

    for(int i = 1; i <= N; i ++)
    {
        fout << (1 - supportLeft[i]) + 2 * (1 - supportRight[i]) << '\n';
    }

    return 0;
}