Cod sursa(job #2689847)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 22 decembrie 2020 14:32:48
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
//ALEXANDRU MICLEA
 
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>
#include <assert.h>
 
using namespace std;
using ll = long long;
 
#include <fstream>
//ifstream cin("input.in"); ofstream cout("output.out");
ifstream cin("biconex.in"); ofstream cout("biconex.out");
 
//VARIABLES

const int maxn = 1e5 + 5;

vector <vector <int>> gr(maxn);
vector <int> ans[maxn];

int used[maxn], tin[maxn], low[maxn];
stack <int> s;

int timer = 0, cont = 0;;

 
//FUNCTIONS

void dfs (int nod, int p){
    used[nod] = true;

    tin[nod] = low[nod] = timer++;
    s.push(nod);

    for (auto& x : gr[nod]){
        if (used[x]) low[nod] = min(low[nod] , tin[x]);
        else {
            dfs(x, nod);
            low[nod] = min(low[nod], low[x]);
            if (low[x] >= tin[nod]){
                cont++;
                while (s.top() != x){
                    ans[cont].push_back(s.top());
                    s.pop();
                }
                ans[cont].push_back(x);
                ans[cont].push_back(nod);
                s.pop();
            } 
        }
    }
}

//MAIN
 
int main() {
    
    int n, m; cin >> n >> m;

    for (int i = 1; i <= m; i++){
        int a, b; cin >> a >> b;

        gr[a].push_back(b);
        gr[b].push_back(a);
    }

    for (int i = 1; i <= n; i++){
        tin[i] = -1;
        low[i] = -1;
    }

    for (int i = 1; i <= n; i++){
        if (!used[i]) dfs(i, 0);
    }

    cout << cont << '\n';
    for (int i = 1; i <= cont; i++){
        for (auto& el : ans[i]){
            cout << el << ' ';
        }
        cout << '\n';
    }

    return 0;
}