Cod sursa(job #2919758)

Utilizator carmenacatrineiCarmen-Lorena Acatrinei carmenacatrinei Data 19 august 2022 13:02:35
Problema Componente tare conexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.79 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;

class graf {

    vector < bool > vis, visInversate;
    vector < vector < int > > muchii, muchiiInversate, solCtc;

    void dfs(int node) {

        vis[node] = true;

        for (int index = 0; index < muchii[node].size(); ++index) {
            int nextNode = muchii[node][index];

            if (!vis[nextNode]) {
                dfs(nextNode);
            }
        }

    }

    void dfsInversate(int node, int nrSol) {

        visInversate[node] = true;
        solCtc[nrSol].push_back(node);

        for (int index = 0; index < muchiiInversate[node].size(); ++index) {
            int nextNode = muchiiInversate[node][index];

            if (!visInversate[nextNode]) {
                dfsInversate(nextNode, nrSol);
            }
        }

    }

    int numarComponenteConexe() {

        int counter = 0;
        int n = muchii.size() - 1;

        vis.resize(n + 1, false);

        for (int node = 1; node <= n; ++node)
            if (!vis[node]) {
                ++counter;
                dfs(node);
            }
        return counter;
    }

    vector < int > bfs(int sursa) {
        int n = muchii.size() - 1;

        queue < int > coadaBfs;
        vector < int > listaBfs;
        listaBfs.resize(n + 1, -1);

        listaBfs[sursa] = 0;
        coadaBfs.push(sursa);

        while (!coadaBfs.empty()) {
            int nod = coadaBfs.front();
            coadaBfs.pop();

            for (int i = 0; i < muchii[nod].size(); ++i)
                if (listaBfs[muchii[nod][i]] == -1) {
                    listaBfs[muchii[nod][i]] = listaBfs[nod] + 1;
                    coadaBfs.push(muchii[nod][i]);
                }
        }

        return listaBfs;
    }

    void dfsSortareTopologica(int nod, stack < int > &topo) {

        vis[nod] = 1;

        for (int i = 0; i < muchii[nod].size(); ++i)
            if (vis[muchii[nod][i]] == 0)
                dfsSortareTopologica(muchii[nod][i], topo);

        topo.push(nod);
    }

    pair < int, vector < vector < int > > > ctc(int n) {

        int nrSol = 0;
        stack < int > topo;

        vis.resize(n + 1, false);
        visInversate.resize(n + 1, false);
        solCtc.resize(n + 1);

        for (int i = 1; i<= n; ++i)
            if (vis[i] == 0)
                dfsSortareTopologica(i, topo);

        while(!topo.empty()) {

            int nod = topo.top();
            topo.pop();

            if (visInversate[nod] == 0) {
                nrSol++;
                dfsInversate(nod, nrSol);
            }

        }

        return make_pair(nrSol, solCtc);
    }

public:

    void dfsInfoarena() {

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

        int n, m, x, y;

        in >> n >> m;

        muchii.resize(n + 1);

        for (int index = 1; index <= m; ++index) {
            in >> x >> y;

            muchii[x].push_back(y);
            muchii[y].push_back(x);
        }

        out << numarComponenteConexe();
    }

    void bfsInfoarena() {

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

        int n, m, nod, x, y;

        in >> n >> m >> nod;

        muchii.resize(n + 1);

        for (int index = 1; index <= m; ++index) {
            in >> x >> y;

            muchii[x].push_back(y);
        }

        vector < int > raspuns = bfs(nod);

        for (int i = 1; i <= n; ++i)
            out << raspuns[i] << " ";
    }

    void sortareTopologicaInfoarena() {

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

        int n, m, x, y;
        stack < int > topo;

        in >> n >> m;

        vis.resize(n + 1, false);
        muchii.resize(n + 1);

        for (int index = 1; index <= m; ++index) {
            in >> x >> y;

            muchii[x].push_back(y);
        }

        for (int i = 1; i<= n; ++i)
            if (vis[i] == 0)
                dfsSortareTopologica(i, topo);

        while (!topo.empty()) {
            int val = topo.top();
            topo.pop();

            out << val << " ";
        }
    }

    void ctcInfoarena() {

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

        int n, m, x, y;

        in >> n >> m;

        muchii.resize(n + 1);
        muchiiInversate.resize(n + 1);

        for (int i = 1; i <= m; ++i) {

            in >> x >> y;

            muchii[x].push_back(y);
            muchiiInversate[y].push_back(x);
        }

        pair < int, vector < vector < int > > > sol = ctc(n);

        out << sol.first << "\n";

        for (int i = 1; i <= sol.first; ++i) {

            int m = sol.second[i].size();

            for (int j = 0; j < m; ++j)
                out << sol.second[i][j] << " ";

            out << "\n";
        }

    }

};

int main()
{
    graf g;

    g.ctcInfoarena();

    return 0;
}