Pagini recente » Cod sursa (job #1463459) | Cod sursa (job #3225428) | Cod sursa (job #124455) | Cod sursa (job #2505069) | Cod sursa (job #781026)
Cod sursa(job #781026)
#include<stdio.h>
#include<assert.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> graph[100005];
vector<vector<int> > ans;
vector<int> stack;
int n,m,vis[100005];
void read(){
assert(freopen("biconex.in","r",stdin));
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i){
int x, y;
scanf("%d%d",&x,&y);
graph[x].push_back(y);
graph[y].push_back(x);
}
}
void dfs(int x){
vis[x] = 1;
stack.push_back(x);
for(int i = 0; i < graph[x].size(); ++i)
if(vis[graph[x][i]]){
int p = graph[x][i];
vector <int> an;
int t = stack.size() - 1;
do{
an.push_back(stack[t]);
--t;
}while(stack[t] != p);
an.push_back(p);
ans.push_back(an);
}
else
dfs(graph[x][i]);
stack.pop_back();
}
void solve(){
for(int i = 1; i <= n; ++i)
if(!vis[i])
dfs(i);
}
void write(){
assert(freopen("biconex.out", "w",stdout));
printf("%d\n",ans.size());
for(int i = 0; i < ans.size(); ++i){
for(int j = 0; j < ans[i].size(); ++j)
printf("%d ",ans[i][j]);
printf("\n");
}
}
int main(){
read();
solve();
write();
}