Cod sursa(job #1405046)

Utilizator teo.serbanescuTeo Serbanescu teo.serbanescu Data 28 martie 2015 19:47:57
Problema Felinare Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;

fstream f("felinare.in", ios::in);
fstream g("felinare.out", ios::out);

const int nmax = 9000;

vector <int> a[nmax];
int n, m, i, used[nmax], l[nmax], r[nmax], st[nmax], dr[nmax];

void read()
{
    int x, y;
    f >> n >> m;
    for (i = 1; i <= m; i++)
    {
        f >> x >> y;
        a[x].push_back(y);
    }
}

int couple(int nc)
{
    if (used[nc]) return 0;
    used[nc] = 1;
    for (vector<int>::iterator it = a[nc].begin(); it != a[nc].end(); ++it)
        if (!r[*it])
        {
            l[nc] = *it;
            r[*it] = nc;
            return 1;
        }
    for (vector<int>::iterator it = a[nc].begin(); it != a[nc].end(); ++it)
        if (couple(r[*it]))
        {
            l[nc] = *it;
            r[*it] = nc;
            return 1;
        }
    return 0;
}

void light(int nc)
{
    for (vector<int>::iterator it = a[nc].begin(); it != a[nc].end(); ++it)
    {
        if (dr[*it])
        {
            dr[*it] = 0;
            st[r[*it]] = 1;
            light(r[*it]);
        }
    }
}

void solve()
{
    int change = 1;
    while (change)
    {
        change = 0;
        memset(used, 0, sizeof(used));
        for (i = 1; i <= n; i++)
            if ((!l[i]) && (couple(i))) change = 1;
    }
    for (i = 1; i <= n; i++)
        if (l[i]) dr[i] = 1;
    for (i = 1; i <= n; i++)
        if (!l[i]) light(i);
    for (i = 1; i <= n; i++)
    {
        if ((!st[i]) && (!dr[i])) g << "0\n";
        if ((st[i]) && (!dr[i])) g << "1\n";
        if ((!st[i]) && (dr[i])) g << "2\n";
        if ((st[i]) && (dr[i])) g << "3\n";
    }
}

int main()
{
    read();
    solve();
    return 0;
}