Cod sursa(job #2470448)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 9 octombrie 2019 11:11:21
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define lsb(x) (x & (-x)) 
    
using namespace std;

const int MAXN = (int) 1e5;

vector <int> g[MAXN + 1];
int lvl[MAXN + 1], low[MAXN + 1];
bool vis[MAXN + 1];
stack <int> stk;

vector <int> comp[MAXN + 1];
int sz;

void dfs(int nod, int par) {
	stk.push(nod), vis[nod] = 1, low[nod] = MAXN + 1;
	for(auto it : g[nod]) {
		if(vis[it] == 0) {
			lvl[it] = lvl[nod] + 1;
			dfs(it, nod);
			low[nod] = min(low[nod], low[it]);
			if(low[it] >= lvl[nod]) {
				++sz;
				while(stk.top() != it) {
					comp[sz].push_back(stk.top());
					stk.pop();
				}
				comp[sz].push_back(it);
				comp[sz].push_back(nod);
				stk.pop();
			}
		}
		else if(it != par) {
			low[nod] = min(low[nod], lvl[it]);
		}
	}
}
   
int main() {
#if 1
    ifstream cin("biconex.in");
    ofstream cout("biconex.out");
#endif
    int i, n, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
	
	cin >> n >> m;
	for(i = 1; i <= m; i++) {
		int x, y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dfs(1, 0);

	cout << sz << "\n";
	for(i = 1; i <= sz; i++) {
		for(auto it : comp[i]) {
			cout << it << " ";
		}
		cout << "\n";
	}

	return 0;
}