Cod sursa(job #2582074)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 16 martie 2020 13:04:25
Problema Componente biconexe Scor 46
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.87 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

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

const int NMax = 100000;

vector <int> g[NMax + 5];
vector <vector <int>> sol;
stack <pair <int, int>> st;
int n, m;
int level[NMax + 5], low[NMax + 5];
bool use[NMax + 5];

void Read(){
    fin >> n >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

void DFS(int node, int father){
    use[node] = 1;
    level[node] = low[node] = level[father] + 1;
    for (auto other : g[node]){
        if (other == father)
            continue;
        if (!use[other]){
            st.push({node, other});
            DFS(other, node);
            if (low[other] >= level[node]){
                bool ok = 1;
                vector <int> aux;
                while (ok){
                    int node1 = st.top().first, node2 = st.top().second;
                    st.pop();
                    if (node1 == node && node2 == other)
                        ok = 0;
                    aux.push_back(node1);
                    aux.push_back(node2);
                }
                sort(aux.begin(), aux.end());
                for (int i = 1; i < aux.size(); i++)
                    if (aux[i] == aux[i - 1])
                        aux.erase(aux.begin() + i);
                sol.push_back(aux);
            }
            low[node] = min(low[node], low[other]);
        }
        else
            low[node] = min(low[node], level[other]);
    }
}

void Print(){
    fout << sol.size() << '\n';
    for (auto i : sol){
        for (auto j : i)
            fout << j << ' ';
        fout << '\n';
    }
}

int main(){
    Read();
    DFS(1, 0);
    Print();
    return 0;
}