Cod sursa(job #3124451)

Utilizator minecraft3Vintila Valentin Ioan minecraft3 Data 28 aprilie 2023 19:12:45
Problema Componente biconexe Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

#define NMAX 100005

int n, m, d[NMAX], low[NMAX];

vector<int> G[NMAX];
stack<pair<int, int>> st;
vector<vector<int>> ans;

void solve(int i, int depth) {
	d[i] = low[i] = depth;
	for (int neigh : G[i]) {
		if (!d[neigh]) {
			st.push({i, neigh});
			solve(neigh, depth + 1);
			low[i] = min(low[neigh], low[i]);
			if (low[neigh] >= d[i]) {
				ans.push_back({});
				pair<int, int> curr;
				do {
					curr = st.top();
					st.pop();
					ans.back().push_back(curr.first);
					ans.back().push_back(curr.second);
				} while (curr != make_pair(i, neigh));
			}
		} else {
			low[i] = min(d[neigh], low[i]);
        }
    }
}

int main()
{
	freopen("biconex.in", "r", stdin);
	freopen("biconex.out", "w", stdout);

    cin >> n >> m;

	for (int i = 1, x, y; i <= m; ++i) {
        cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	for (int i = 1; i <= n; ++i)
		if (!d[i]) solve(i, 1);
    
    cout << ans.size() << endl;
	for (vector<int> &v : ans) {
		sort(v.begin(), v.end());
		v.erase(unique(v.begin(), v.end()), v.end());
		for (int i : v)
            cout << i << ' ';
        cout.put('\n');
	}
}