Pagini recente » Cod sursa (job #362327) | Cod sursa (job #243159) | Cod sursa (job #1220658) | Cod sursa (job #1583804) | Cod sursa (job #2174088)
#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;
bitset <Nmax> in_stack;
void DFS(int x)
{
dfn[x]=low[x]=++N;
st.push(x);
in_stack[x]=true;
for(const auto &it:G[x])
{
if(dfn[it]==-1)
{
DFS(it);
low[x]=min(low[x],low[it]);
}
else if(in_stack[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();
in_stack[node]=false;
}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);
for(i=1;i<=n;i++)
if(dfn[i]==-1) DFS(i);
g<<ans<<'\n';
for(i=1;i<=n;i++,g<<'\n')
for(const auto &it:ctc[i])
g<<it<<' ';
return 0;
}