Pagini recente » Cod sursa (job #1896811) | Cod sursa (job #2839310) | Cod sursa (job #3220381) | Cod sursa (job #3220390) | Cod sursa (job #3285088)
#include <fstream>
#include <vector>
#include <stack>
#define nmax (int)(1e5+1)
using namespace std;
ifstream cin("ctc.in");
ofstream cout("ctc.out");
int n,m,x,y,k,niv[nmax],minlv[nmax];
bool viz[nmax],in[nmax];
vector<int>v[nmax],aux;
vector<vector<int>>sol;
stack<int>s;
void dfs(int nod){
viz[nod]=1;
s.push(nod);
in[nod]=1;
minlv[nod]=niv[nod]=++k;
for(auto i:v[nod])
if(!viz[i]){
dfs(i);
if(in[i])
minlv[nod]=min(minlv[nod],minlv[i]);
}else if(in[i])
minlv[nod]=min(minlv[nod],niv[i]);
if(niv[nod]==minlv[nod]){
aux.clear();
while(!s.empty()&&s.top()!=nod){
aux.push_back(s.top());
in[s.top()]=0;
s.pop();
}
in[nod]=0;
s.pop();
aux.push_back(nod);
sol.push_back(aux);
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>y;
v[x].push_back(y);
}
for(int i=1;i<=n;i++)
if(!viz[i])
dfs(i);
cout<<sol.size()<<'\n';
for(auto i:sol){
for(auto j:i)
cout<<j<<" ";
cout<<'\n';
}
return 0;
}
/// Tarjan