Pagini recente » Cod sursa (job #2684379) | Cod sursa (job #1344818) | Cod sursa (job #1545050) | Cod sursa (job #356711) | Cod sursa (job #2174048)
#include <bits/stdc++.h>
#define Nmax 100001
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
list <int> G[Nmax];
int n,m,N,ans,node;
list <int> ctc[Nmax];
int dfn[Nmax];
int low[Nmax];
stack <int> st;
void DFS(int x)
{
dfn[x]=low[x]=++N;
st.push(x);
for(const auto &it:G[x])
{
if(dfn[it]==-1) DFS(it);
low[x]=min(low[x],low[it]);
}
if(low[x]==dfn[x])
{
++ans;
do
{
node=st.top();
ctc[ans].push_back(node);
st.pop();
}while(node!=x);
}
}
int main()
{
int i,j;
f>>n>>m;
while(m--)
{
f>>i>>j;
G[i].push_back(j);
}
fill(dfn+1,dfn+n+1,-1);
fill(low+1,low+n+1,-1);
DFS(1);
g<<ans<<'\n';
for(i=1;i<=n;i++,g<<'\n')
for(const auto &it:ctc[i])
g<<it<<' ';
return 0;
}