Cod sursa(job #2528321)

Utilizator DariusDCDarius Capolna DariusDC Data 21 ianuarie 2020 19:10:47
Problema Felinare Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 10005;
int n, m;
vector <int> g[nmax];
int st[nmax], dr[nmax], viz[nmax];
int f1[nmax], f2[nmax];
int c;

inline bool check(int nod)
{
    if (viz[nod])
        return false;
    viz[nod] = 1;
    for (auto it : g[nod])
    {
        if (!dr[it])
        {
            st[nod] = it;
            dr[it] = nod;
            return true;
        }
    }
    for (auto it : g[nod])
    {
        if (viz[it] == 0 && check(dr[it]))
        {
            st[nod] = it;
            dr[it] = nod;
            return 1;
        }
    }
    return 0;
}

inline void cuplaj()
{
    int ok = 1;
    while (ok)
    {
        ok = 0;
        for (int i = 1; i <= n; i++)
            viz[i] = 0;
        for (int i = 1; i <= n; i++)
            if (!st[i] && check(i))
                c++, ok = 1;
    }
}

inline void dfs(int nod)
{
    for (auto it : g[nod])
    {
        if (!f2[it])
        {
            f2[it] = 1;
            f1[dr[it]] = 0;
            dfs(dr[it]);
        }
    }
}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        fin >> a >> b;
        g[a].push_back(b);
    }
    cuplaj();
    fout << 2*n - c << "\n";
    for (int i = 1; i <= n; i++)
        if (st[i])
            f1[i] = 1;
    for (int i = 1; i <= n; i++)
        if (!st[i])
            dfs(i);
    for (int i = 1; i <= n; i++)
        fout << 3 - f1[i] - 2*f2[i] << "\n";
    return 0;
}