Pagini recente » Cod sursa (job #296780) | Cod sursa (job #3004170) | Cod sursa (job #671879) | Cod sursa (job #2392960) | Cod sursa (job #2709667)
#include <bits/stdc++.h>
using namespace std;
ifstream f("biconex.in");
ofstream g("biconex.out");
stack <pair <int, int>> st;
vector <unordered_set <int>> bcc;
vector <int> gr[100001];
int tin[100001], low[100001];
int N, M;
void Read(){
f >> N >> M;
while(M--){
int x, y;
f >> x >> y;
gr[x].emplace_back(y);
gr[y].emplace_back(x);
}
}
void find_bcc(int x, int y = 0){
unordered_set <int> con;
int tx, ty;
do{
tx = st.top().first, ty = st.top().second;
st.pop();
con.insert(tx), con.insert(ty);
}while(tx != x || ty != y);
bcc.emplace_back(con);
}
void dfs(int v = 1, int p = 0){
low[v] = tin[v] = tin[p] + 1;
for(int to : gr[v]){
if(to == p) continue;
if(!tin[to]){
st.push({v, to});
dfs(to, v);
low[v] = min(low[v], low[to]);
if(low[to] >= tin[v])
find_bcc(v, to);
}
else low[v] = min(low[v], tin[to]);
}
}
void Print_bcc(){
g << bcc.size() << "\n";
for(int i = 0;i < (int)bcc.size();i++){
for(int x : bcc[i])
g << x << " ";
g << "\n";
}
}
int main(){
Read();
dfs(1, 0);
Print_bcc();
}