Cod sursa(job #2215001)

Utilizator silviu982001Borsan Silviu silviu982001 Data 20 iunie 2018 18:16:52
Problema Felinare Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

#define NMAX 8193
#define MMAX 20001

int n, m;
vector<int> G[NMAX];
bitset<NMAX> visited, supl, supr;
int nodl[NMAX], nodr[NMAX];

bool coupling(int nod)
{
    if (visited[nod])
        return false;
    visited[nod] = 1;
    for (auto v : G[nod])
    {
        if (nodr[v] == 0)
        {
            nodr[v] = nod;
            nodl[nod] = v;
            return true;
        }
    }
    
    for (auto v : G[nod])
    {
        if (coupling(nodr[v]))
        {
            nodr[v] = nod;
            nodl[nod] = v;
            return true;
        }
    }
    
    return false;
}

void support(int nod)
{
    for (auto v : G[nod])
        if (supr[v] == 0)
        {
            supr[v] = 1;
            supl[nodr[v]] = 0;
            support(nodr[v]);
        }
}

int main()
{
    ifstream fin("felinare.in");
    fin >> n >> m;
    int x, y;
    for (int i = 0; i < m; i++)
    {
        fin >> x >> y;
        G[x].push_back(y);
    }
    fin.close();
    
    bool ok;
    do
    {
        ok = false;
        for (int i = 1; i <= n; i++)
            if (!nodl[i])
                ok |= coupling(i);
    }
    while (ok);
    
    for (int i = 1; i <= n; i++)
        if (nodl[i])
            supl[i] = 1;
    
    for (int i = 1; i <= n; i++)
        if (!supl[i])
            support(i);
    
    int sol = (n << 1);
    for (int i = 1; i <= n; i++)
    {
        if (supl[i])
            --sol;
        if (supr[i])
            --sol;
    }
    ofstream fout("felinare.out");
    fout << sol << '\n';
    for (int i = 1; i <= n; i++)
        fout << 3 - supl[i] - (2 * supr[i]) << '\n';
    fout.close();
    return 0;
    
    
}