Cod sursa(job #2595803)

Utilizator tudorcioc5Cioc Tudor tudorcioc5 Data 8 aprilie 2020 14:07:42
Problema Componente biconexe Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.81 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
using namespace std;

ifstream fin;
ofstream fout;

bitset <100001> visited;
vector <vector<int>> neighbours (100001);
vector <int> level (100001);
vector <int> minimum_accessible (100001);
stack <int> biconnected_stack;
vector <vector<int>> biconnected_components;
int nodes, edges, j, k, task;

void dfs_search (int where_to_start, int parent){
    visited[where_to_start] = true;
    level[where_to_start] = level[parent] + 1;
    minimum_accessible[where_to_start] = where_to_start;

    biconnected_stack.push(where_to_start);

    for (vector<int>::iterator it = neighbours[where_to_start].begin(); it != neighbours[where_to_start].end(); it++)
        if (*it != parent){
            if (!visited[*it]){

                dfs_search(*it, where_to_start);
                if (minimum_accessible[*it] < minimum_accessible[where_to_start])
                    minimum_accessible[where_to_start] = minimum_accessible[*it];

                if (level[where_to_start] <= minimum_accessible[*it]){
                    vector <int> aux;
                    while (biconnected_stack.top() != *it)
                        aux.push_back(biconnected_stack.top()) , biconnected_stack.pop();

                    biconnected_stack.pop();
                    aux.push_back(*it);
                    aux.push_back(where_to_start);
                    biconnected_components.push_back(aux);
                }

            }

            else if (level[*it] < minimum_accessible[where_to_start])
                minimum_accessible[where_to_start] = level[*it];


        }

}

int main (void){
    fin.open("biconex.in");
    fin>>nodes>>edges;
    for (int i=1; i<=edges; i++){
        fin>>j>>k;
        neighbours[j].push_back(k);
        neighbours[k].push_back(j);
    }
    fin.close();

    minimum_accessible[1] = 1;
    level[1] = 1;
    visited[1] = true;
    for (vector<int>::iterator it = neighbours[1].begin(); it != neighbours[1].end(); it++)
        if (!visited[*it]){
            biconnected_stack.push(1);
            dfs_search(*it, 1);
            if (biconnected_stack.size() > 1){
                vector <int> aux;
                while(!biconnected_stack.empty())
                    aux.push_back(biconnected_stack.top()), biconnected_stack.pop();

                biconnected_components.push_back(aux);
            }
            else while (!biconnected_stack.empty()) biconnected_stack.pop();
        }

    fout.open("biconex.out");
    fout<<biconnected_components.size()<<"\n";
    for (vector <vector<int>>::iterator it1= biconnected_components.begin(); it1 != biconnected_components.end(); it1++){
        for (vector<int>::iterator it2 = it1->begin(); it2 != it1->end(); it2++)
            fout<<*it2<<" ";
        fout<<"\n";
    }
    fout.close();


    return 0;
}