Cod sursa(job #2806361)

Utilizator andreic06Andrei Calota andreic06 Data 22 noiembrie 2021 16:01:49
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.86 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <ctype.h>

using namespace std;
const int NMAX = 1e5;

int low_link[1 + NMAX]; bool visited[1 + NMAX];
vector<int> graph[1 + NMAX];

/// nu trebuia dar in fine
struct manual_stack {
      int value[1 + NMAX];
      bool in[1 + NMAX];

      int st_size = 0;

      void my_push ( int x ) {
          value[st_size ++] = x;
      }
      void my_pop () {
          st_size --;
      }
      int top () {
         return value[st_size - 1];
      }

      void get_in ( int x ) {
         in[x] = 1;
      }
      void get_out ( int x ) {
          in[x] = 0;
      }
      bool in_stack ( int x ) {
         return in[x];
      }

} st;

vector<int> conex[1 + NMAX]; int total = 0;
int index = 1;
void dfs ( int root ) {
    visited[root] = true;
    low_link[root] = root; st.get_in ( root );
    for ( auto node : graph[root] ) {
       if ( !visited[node] ) {
         dfs ( node );
         low_link[root] = min ( low_link[root], low_link[node] );
       }
       else if ( st.in_stack ( node ) )
         low_link[root] = min ( low_link[root], low_link[node] );
    }

    if ( low_link[root] == root ) {

      do {
        conex[total].push_back ( st.top () );
        st.get_out ( st.top () );
      } while ( st.top () != root );

      total ++;
    }

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

int main()
{
   int n, m; fin >> n >> m;
   for ( int i = 1; i <= m; i ++ ) {
      int u, v; fin >> u >> v;
      graph[u].push_back ( v );
      graph[v].push_back ( u );
   }
   for ( int i = 1; i <= n; i ++ )
      if ( !visited[i] )
        dfs ( i );
   for ( int i = 1; i <= total; i ++ )
      for ( int this_size = 0; this_size < (int) conex.size (); this_size ++ )
         fout << conex[i][this_size];
    return 0;
}