Cod sursa(job #2561198)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 28 februarie 2020 17:36:25
Problema Felinare Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, m;
bool f[8200], l[8200], r[8200];
int st[8200], dr[8200];
vector <int> g[8200];

inline bool cupleaza(int nod) {
    if (f[nod])
        return 0;
    f[nod] = 1;
    for (auto vecin : g[nod]) {
        if (!st[vecin]) {
            dr[nod] = vecin;
            st[vecin] = nod;
            return 1;
        }
        if (!f[st[vecin]] && cupleaza(st[vecin])) {
            dr[nod] = vecin;
            st[vecin] = nod;
            return 1;
        }
    }
    return 0;
}

inline void DFS(int nod) {
    for (auto vecin : g[nod]) {
        if (!l[vecin]) {
            l[vecin] = 1;
            l[st[vecin]] = 0;
            DFS(st[vecin]);
        }
    }
}

int main() {
    int x, y;
    in >> n >> m;
    for (int i = 1; i <= m; ++i) {
        in >> x >> y;
        g[x].push_back(y);
    }
    bool ok = 1;
    while (ok) {
        memset(f, 0, sizeof(f));
        ok = 0;
        for (int i = 1; i <= n; ++i) {
            if (!f[i] && !dr[i] && cupleaza(i))
                ok = 1;
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (dr[i])
            r[i] = 1;
    }
    for (int i = 1; i <= n; ++i) {
        if (!dr[i])
            DFS(i);
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
        if (!l[i])
            ++cnt;
        if (!r[i])
            ++cnt;
    }
    out << cnt << '\n';
    for (int i = 1; i <= n; ++i) {
        if (!l[i] && !r[i])
            out << 3;
        else if (!r[i])
            out << 1;
        else if (!l[i])
            out << 2;
        else
            out << 0;
        out << '\n';
    }
    return 0;
}