Cod sursa(job #2785837)

Utilizator MirunaStefaniaLupascu Miruna-Stefania MirunaStefania Data 19 octombrie 2021 17:31:30
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.83 kb
#include <iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<stack>
#define N 100005


using namespace std;

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

queue<int> q;

int ctComponents = 0;
vector<int> comp[N]; //to print the strongly connected components

class Graph {
private:
    int n, m;

    vector<int> adlist[N];
    bool viz[N] = {0};
    int dist[N] = {0};

    stack<int> s; //final times in dfs
public:
    void readDirected();
    void readUndirected();

    Graph() = default;
    Graph(int n, int m):n(n), m(m){}

    void bfs(int first);
    void dfs(int first);
    void dfsT(int first); //used only for Transpoused --> do not add in s, print the node
                          //we will use "viz" from the transpoused graph
    void printDist();
    void connectedComponents();
    void printGraph();
    Graph transpose();
    void stronglyConnectedComponents();

};



void Graph::readDirected()
{
    int i, x, y;
    for(i = 1; i <= m; ++i)
    {
        fin >> x >> y;
        adlist[x].push_back(y);

    }
}


void Graph::readUndirected() {
    int i, x, y;
    for(i = 1; i <= m; ++i)
    {
        fin >> x >> y;
        adlist[x].push_back(y);
        adlist[y].push_back(x);

    }
}

void Graph::printGraph() {
    int i, j;
    for(i = 1; i <= n; ++i) {
        fout << i <<":";
        for(j = 0; j < adlist[i].size(); ++j)
            fout << adlist[i][j] << " ";
        fout <<"\n";

    }
}

void Graph::bfs(int first){
    int node, i, dim;
    dist[first] = 1;
    viz[first] = 1;
    q.push(first);

    while(!q.empty())
    {
        node = q.front();
        q.pop();
        dim = adlist[node].size();
        for(i = 0; i < dim; ++i)
            if(!viz[adlist[node][i]])
        {
            cout << adlist[node][i] <<" ";
            viz[adlist[node][i]] = 1;
            dist[adlist[node][i]] = dist[node] + 1;
            q.push(adlist[node][i]);
        }
    }
}

void Graph::dfs(int node){
    int i, dim;
    viz[node] = 1;
    dim = adlist[node].size();
    for(i = 0; i < dim; ++i)
            if(!viz[adlist[node][i]])
                dfs(adlist[node][i]);
    s.push(node);
}
void Graph::dfsT(int node){
    int i, dim;

    comp[ctComponents].push_back(node);//is part of the smae component

    viz[node] = 1;
    dim = adlist[node].size();
    for(i = 0; i < dim; ++i)
            if(!viz[adlist[node][i]])
                dfsT(adlist[node][i]);

}

void Graph::printDist(){
    int i;
    for(i = 1; i <= n; ++i)
        fout << dist[i] - 1 << " ";
}

void Graph::connectedComponents() {
    int i, nr = 0;
    for(i = 1; i <= n; ++i)
        if(!viz[i])
    {
        nr++;
        dfs(i);
    }
    fout << nr;

}

Graph Graph::transpose() {
    int i, j;
    Graph gt(n, m);
    for(i = 1; i <= n; ++i)
        for(j = 0; j < adlist[i].size(); ++j)
            gt.adlist[adlist[i][j]].push_back(i);
    return gt;

}

void Graph::stronglyConnectedComponents(){


    int node;
    this->dfs(1);
    Graph gt = this->transpose();
    while(!s.empty())
    {
        node = s.top();
        s.pop();
        if(!gt.viz[node])
            {
                ctComponents++;
                gt.dfsT(node);

            }

    }

    int i, j;
    fout << ctComponents << "\n";
    for(i = 1; i <= ctComponents; ++i) {
        for(j = 0; j < comp[i].size(); ++j)
            fout <<comp[i][j] << " ";
        fout << "\n";
            }

}
int main()
{
    int i, first, n, m;

    fin >> n >> m;
    Graph g(n, m);


    /*
    g.readOriented();
    g.bfs(first);
    g.printDist();
*/
/*
    g.readUndirected();
    g.connectedComponents();
*/
    g.readDirected();
    g.stronglyConnectedComponents();




    return 0;
}