Cod sursa(job #3366363)

Utilizator robertcosacCosac Robert-Mihai robertcosac Data 30 septembrie 2026 21:31:05
Problema Felinare Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("felinare.in");
ofstream g("felinare.out");
vector <int> v[20009];
int st[20009], dr[20009], tried[20009];
int n, m, q, ans;
bool lin[20000], col[20009], viz[20009];
bool augment (int nod)
{
    if (tried[nod])
        return 0;
    tried[nod]=1;
    //cout << nod << ' ';
    for (auto y:v[nod])
    {
        if (!st[y])
        {
            dr[nod]=y;
            st[y]=nod;
            ans++;
            return 1;
        }
    }
    for (auto y:v[nod])
    {
        if (augment (st[y]))
        {
            st[y]=nod;
            dr[nod]=y;
            return 1;
        }
    }
    return 0;
}
void cuplaj ()
{
    bool ok=1;
    while (ok)
    {
        ok=0;
        for (int i=1; i<=n; i++)
            tried[i]=0;
        for (int i=1; i<=n; i++)
            if (!dr[i]) ok|=augment(i);
    }
}
void mvc (int nod)
{
    lin[nod]=0;
    viz[nod]=1;
    for (auto y:v[nod])
    {
        if (!viz[st[y]])
        {
            col[y]=1;
            mvc (st[y]);
        }
    }
}
signed main ()
{
    f >> n >> m;
    while (m--)
    {
        int x, y;
        f >> x >> y;
        v[x].push_back(y);
    }
    cuplaj ();
    for (int i=1; i<=n; i++)
        if (dr[i]) lin[i]=1;
    for (int i=1; i<=n; i++)
    {
        if (!viz[i] && !lin[i])
            mvc (i);
    }
    g << 2*n-ans<<'\n';
    for (int i=1; i<=n; i++)
    {
        if (lin[i] && col[i])
            g << 0;
        else if (col[i])
            g << 1;
        else if (lin[i])
            g << 2;
        else g << 3;
        g <<'\n';
    }
}