Cod sursa(job #2793598)

Utilizator Radu_FilipescuFilipescu Radu Radu_Filipescu Data 3 noiembrie 2021 19:44:20
Problema Componente tare conexe Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 5.5 kb
#include <bits/stdc++.h>

using namespace std;

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

class graph {
private:
    int N;                      /// number of nodes
    int t = 0;                  /// auxiliary variable, for different needs
    vector< vector<int> > ad;
    vector< vector<int> > reverseAd;
    vector< vector<int> > cost;
    vector<int> dist;
    vector<int> low;
    vector<int> aux;            /// auxiliary vector, for different needs
    int conexComponents;        /// requires DFSTraversal to set the value
    void Dfs( int node ) {
        aux.push_back( node );
        dist[node] = 1;

        for( int i = 0; i < ad[node].size(); ++i ) {
            int w = ad[node][i];

            if( dist[w] == 0 )
                Dfs( w );
        }
    }
    void Dfs2( int node, int parent, vector<vector<int> > &_solution ) {
        dist[node] = low[node] = ++t;
        aux.push_back( node );

        for( int i = 0; i < ad[node].size(); ++i ) {
           int w = ad[node][i];

           if( w == parent ) continue;

           if( dist[w] > 0 ) low[node] = min( low[node], dist[w] );
           else {
              Dfs2( w, node, _solution );
              low[node] = min( low[node], low[w] );

              if( low[w] >= dist[node] ) {
                 vector <int> tmp;

                 while( aux.back() != w ) {
                    tmp.push_back( aux.back() );
                    aux.pop_back();
                 }
                 tmp.push_back( aux.back() );
                 aux.pop_back();

                 tmp.push_back( node );
                 _solution.push_back( tmp );
              }
           }
        }
    }
    void Dfs3( int node ) {
        dist[node] = 1;

        for( int i = 0; i < ad[node].size(); ++i ) {
            int w = ad[node][i];

            if( dist[w] == 0 )
                Dfs3( w );
        }

        aux.push_back( node );
    }
    void Dfs4( int node, vector<int> &v ) {
        dist[node] = 1;
        v.push_back( node );

        for( int i = 0; i < reverseAd[node].size(); ++i ) {
            int w = reverseAd[node][i];

            if( dist[w] == 0 )
                Dfs4( w, v );
        }
    }
public:
    graph( int n ) {
        N = n;
        ad.resize( n + 1 );
        cost.resize( n + 1 );
        dist.resize( n + 1 );
        low.resize( n + 1 );
        reverseAd.resize( n + 1 );
    }
    void changeN( int n ) {
        N = n;
        ad.resize( n + 1 );
        cost.resize( n + 1 );
        dist.resize( n + 1 );
        low.resize( n + 1 );
        reverseAd.resize( n + 1 );
    }
    int getN() { return N; }
    void addEdge( int x, int y, bool directed = false, int c = 0 ) {
        ad[x].push_back(y);
        cost[x].push_back(c);

        reverseAd[y].push_back(x);

        if( !directed ) {
            ad[y].push_back(x);
            cost[y].push_back(c);
            reverseAd[x].push_back(y);
        }
    }
    vector <int> DfsTraversal() {
        aux.clear();
        conexComponents = 0;

        for( int i = 1; i <= N; ++i )
            dist[i] = 0;

        for( int i = 1; i <= N; i++ )
            if( dist[i] == 0 ) {
                Dfs( i );
                ++conexComponents;
            }

        return aux;
    }
    void BfsTraversal( int source ) {
        queue<int> Q;

        for( int i = 0; i <= N; i++ )
            dist[i] = 0;

        dist[source] = 1;
        Q.push( source );

        while( !Q.empty() ) {
            int u = Q.front();
            Q.pop();

            for( int i = 0; i < ad[u].size(); ++i ) {
                int w = ad[u][i];

                if( dist[w] == 0 ) {
                    dist[w] = dist[u] + 1;
                    Q.push( w );
                }
            }
        }
    }
    vector<int>getTopologicalSort() {
        aux.clear();
        for( int i = 1; i <= N; i++ )
            dist[i] = 0;

        for( int i = 1; i <= N; i++ )
            if( dist[i] == 0 )
                Dfs3( i );

        return aux;
    }
    vector<vector<int> > getStronglyConnectedComponents() {
        vector<vector<int> > sol;
        vector<int> topologicalSort = getTopologicalSort();

        for( int i = 0; i <= N; i++ )
            dist[i] = 0;


        for( int i = topologicalSort.size() - 1; i >= 0; --i ) {
            int node = topologicalSort[i];

            if( dist[node] ) continue;

            vector<int> tmp;

            Dfs4( node, tmp );

            sol.push_back( tmp );
        }

        return sol;
    }
    vector<vector<int> > getBiconnectedComponents() {
        vector<vector<int> > sol;

        for( int i = 1; i <= N; ++i )
            if( dist[i] == 0 )
                Dfs2( i, 0, sol );

        return sol;
    }
    int getConexComponents() {    /// requires DFS to be called first
        return conexComponents;
    }
    vector <int> getDistances() { /// requires BFS to be called first
        return dist;
    }
};


int main()
{
    int n, m;
    fin >> n >> m;

    graph G(n);

    int x, y;
    for( int i = 1; i <= m; i++ ) {
        fin >> x >> y;
        G.addEdge( x, y, true );
    }


    vector<vector<int> > tmp = G.getStronglyConnectedComponents();

    fout << tmp.size() << '\n';
    for( int i = 0; i < tmp.size(); ++i ) {
        for( int j = 0; j < tmp[i].size(); ++j )
            fout << tmp[i][j] << ' ';
        fout << '\n';
    }

    return 0;
}