Pagini recente » Cod sursa (job #2786917) | Cod sursa (job #643214) | Cod sursa (job #2519805) | Cod sursa (job #2354754) | Cod sursa (job #2715832)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int n, m, low[100005], lev[100005], v[100005];
vector<int> g[100005];
vector<vector<int> > comp;
stack<pair<int, int> > st;
void add(int x, int y) {
vector<int> c;
while((st.top().first != x || st.top().second != y) && !st.empty()) {
c.push_back(st.top().first);
c.push_back(st.top().second);
st.pop();
}
if(st.top().first == x && st.top().second == y) {
st.pop();
c.push_back(x);
c.push_back(y);
}
sort(c.begin(), c.end());
comp.push_back(c);
}
void dfs(int x, int t) {
v[x] = 1;
low[x] = lev[x] = lev[t]+1;
for(auto next: g[x]) {
if(!v[next]) {
st.push({x, next});
dfs(next, x);
if(low[next] >= low[x])
add(x, next);
low[x] = min(low[x], low[next]);
} else if(next != t)
low[x] = min(low[x], lev[next]);
}
}
int main() {
fin >> n >> m;
while(m--) {
int u, v;
fin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
fout << comp.size() << '\n';
for(auto c: comp) {
for(int i = 0; i < c.size(); i++)
if(i == 0 || c[i] != c[i-1])
fout << c[i] << ' ';
fout << '\n';
}
}