Cod sursa(job #2555730)

Utilizator KappaClausUrsu Ianis Vlad KappaClaus Data 24 februarie 2020 11:53:25
Problema Componente biconexe Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.55 kb
#include <bits/stdc++.h>
#define N_MAX 100000 + 1
using namespace std;

ifstream fin{"biconex.in"};
ofstream fout{"biconex.out"};

vector<vector<int>> graph(N_MAX, vector<int>());
int N, M;

vector<int> id(N_MAX, 0);
vector<int> low(N_MAX, 0);
vector<bool> visited(N_MAX, false);
vector<vector<int>> ans;
stack<int> node_stack;

int __timer__ = 0;

void DFS(int node, int parent)
{
    visited[node] = true;

    id[node] = low[node] = ++__timer__;

    node_stack.push(node);

    for(auto& next : graph[node])
    {
        if(next == parent) continue;

        if(visited[next] == false){

            DFS(next, node);

            low[node] = min(low[node], low[next]);

            if(id[node] <= low[next])
            {
                vector<int> bcc;

                while(node_stack.top() != next)
                {
                    bcc.push_back(node_stack.top());
                    node_stack.pop();
                }

                bcc.push_back(next);
                bcc.push_back(node);

                ans.push_back(bcc);
            }
        }
        else
            low[node] = min(low[node], id[next]);
    }
}


int main()
{
    fin >> N >> M;

   for(int i = 1; i <= M; ++i)
   {
       int x, y;

       fin >> x >> y;

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

   DFS(1, -1);

   fout << ans.size() << '\n';

   for(auto& bcc : ans)
   {
       for(auto& node : bcc)
       {
           fout << node << ' ';
       }

       fout << '\n';
   }
}