Cod sursa(job #3228997)

Utilizator AlinSSimilea Alin-Andrei AlinS Data 12 mai 2024 20:39:56
Problema Componente tare conexe Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4 kb
#include <bits/stdc++.h>
using namespace std;

class Task {
public:
    void solve() {
        read_input();
        print_output(get_result());
    }

private:
    // numarul maxim de noduri
    static constexpr int NMAX = (int)1e5 + 5; // 10^5 + 5 = 100.005

    // n = numar de noduri, m = numar de muchii/arce
    int n, m;

    // adj[node] = lista de adiacenta a nodului node
    // exemplu: daca adj[node] = {..., neigh, ...} => exista arcul (node, neigh)
    vector<int> adj[NMAX];

    void read_input() {
        ifstream fin("ctc.in");
        fin >> n >> m;
        for (int i = 1, x, y; i <= m; i++) {
            fin >> x >> y;
            adj[x].push_back(y); // arc (x, y)
        }
        fin.close();
    }

    bool stack_contains(stack<int> st, int node) {
        while (!st.empty()) {
            if (st.top() == node) {
                return true;
            }
            st.pop();
        }
        return false;
    }

    void tarjan_scc_dfs(int node, vector<int>& parent, int& timestamp, vector<int>& found, vector<int>& low_link,
        stack<int>& stack, vector<vector<int>>& all_sccs) {
        // new node visited
        found[node] = timestamp++;
        low_link[node] = found[node];
        stack.push(node);

        // visit neighbours
        for (auto neigh : adj[node]) {
            if (parent[neigh] != -1) { // already visited

                if (stack_contains(stack, neigh)) {
                    low_link[node] = min(low_link[node], found[neigh]);
                }
                continue;
            }

            // not visited
            parent[neigh] = node;
            tarjan_scc_dfs(neigh, parent, timestamp, found, low_link, stack, all_sccs);
            low_link[node] = min(low_link[node], low_link[neigh]);
        }

        // beginning of a SCC
        if (found[node] == low_link[node]) {
            vector<int> scc;
            // SCC from top of stack to current node
            while (stack.top() != node) {
                scc.push_back(stack.top());
                stack.pop();
            }
            scc.push_back(node);
            stack.pop();
            all_sccs.push_back(scc);
        }
    }

    void tarjan_scc(vector<vector<int>>& all_sccs) {
        vector<int> parent(n + 1, -1);
        vector<int> found(n + 1, INT_MAX);
        vector<int> low_link(n + 1, INT_MAX);

        stack<int> s;

        int timestamp = 0;
        for (int i = 1; i <= n; i++) {
            if (parent[i] == -1) {
                parent[i] = i;
                tarjan_scc_dfs(i, parent, timestamp, found, low_link, s, all_sccs);
            }
        }
    }

    vector<vector<int>> get_result() {
        //
        // TODO: Găsiți componentele tare conexe  (CTC / SCC) ale grafului orientat cu n noduri, stocat în adj.
        //
        // Rezultatul se va returna sub forma unui vector, fiecare element fiind un SCC (adică tot un vector).
        // * nodurile dintr-un SCC pot fi găsite în orice ordine
        // * SCC-urile din graf pot fi găsite în orice ordine
        //
        // Indicație: Folosiți algoritmul lui Tarjan pentru SCC.
        //

        vector<vector<int>> all_sccs;
        tarjan_scc(all_sccs);
        return all_sccs;
    }

    void print_output(const vector<vector<int>>& all_sccs) {
        ofstream fout("ctc.out");
        fout << all_sccs.size() << '\n';
        for (const auto& scc : all_sccs) {
            for (auto node : scc) {
                fout << node << ' ';
            }
            fout << '\n';
        }
        fout.close();
    }
};

// [ATENTIE] NU modifica functia main!
int main() {
    // * se aloca un obiect Task pe heap
    // (se presupune ca e prea mare pentru a fi alocat pe stiva)
    // * se apeleaza metoda solve()
    // (citire, rezolvare, printare)
    // * se distruge obiectul si se elibereaza memoria
    auto* task = new (nothrow) Task(); // hint: cppreference/nothrow
    if (!task) {
        cerr << "new failed: WTF are you doing? Throw your PC!\n";
        return -1;
    }
    task->solve();
    delete task;
    return 0;
}