Cod sursa(job #2583518)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 18 martie 2020 13:43:54
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 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], sol[NMax + 5];
stack <pair <int, int>> st;
int n, m, nr;
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;
                ++nr;
                while (ok){
                    int node1 = st.top().first, node2 = st.top().second;
                    st.pop();
                    if (node1 == node && node2 == other)
                        ok = 0;
                    sol[nr].push_back(node2);
                }
                sol[nr].push_back(node);
            }
            low[node] = min(low[node], low[other]);
        }
        else
            low[node] = min(low[node], level[other]);
    }
}

void Print(){
    fout << nr << '\n';
    for (int i = 1; i <= nr; i++){
        for (auto j : sol[i])
            fout << j << ' ';
        fout << '\n';
    }
}

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