Pagini recente » Cod sursa (job #3191616) | Cod sursa (job #890671) | Cod sursa (job #1586555) | Cod sursa (job #684818) | Cod sursa (job #2279471)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define pb push_back
#define NM 100005
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
vector<int> G[NM],GT[NM];
vector<int> sol[NM];
stack<int> st;
int n,m,a,b,nrs;
int viz[NM];
void dfs(int nod)
{
viz[nod]=1;
for(auto fiu:G[nod])
if(!viz[fiu])
dfs(fiu);
st.push(nod);
}
void dfst(int nod)
{
viz[nod]=1;
sol[nrs].pb(nod);
for(auto fiu:GT[nod])
if(!viz[fiu])
dfst(fiu);
}
int main()
{
in>>n>>m;
for(int i=1;i<=m;++i)
{
in>>a>>b;
G[a].pb(b);
GT[b].pb(a);
}
for(int i=1;i<=n;++i)
if(!viz[i])
dfs(i);
for(int i=1;i<=n;++i)
viz[i]=0;
while(!st.empty())
{
while(!st.empty() && viz[st.top()])st.pop();
if(!st.empty())
{
++nrs;
dfst(st.top());
st.pop();
}
}
out<<nrs<<'\n';
for(int i=1;i<=nrs;++i)
{
for(auto nod:sol[i])out<<nod<<" ";
out<<'\n';
}
return 0;
}