Pagini recente » Cod sursa (job #2455611) | Cod sursa (job #2948506) | Cod sursa (job #2304702) | Cod sursa (job #1875102) | Cod sursa (job #2717635)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
stack <int> S;
vector <int> G[100001],GT[100001];
vector <int> ctc[100001];
int n,m,cnt,x,y;
short int viz[100001];
void DFS_T(int nod_curent)
{
viz[nod_curent]=2;
ctc[cnt].push_back(nod_curent);
for(auto& nod:GT[nod_curent])
if(viz[nod]==1)
DFS_T(nod);
}
void DFS(int nod_curent)
{
viz[nod_curent]=1;
for(auto& nod:G[nod_curent])
if(!viz[nod])
DFS(nod);
S.push(nod_curent);
}
int main()
{
f>>n>>m;
for(int i=1; i<=m; i++)
{
f>>x>>y;
G[x].push_back(y);
GT[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(viz[i]!=1)
DFS(i);
while(!S.empty())
{
int nod=S.top();
if(viz[nod]==1)
{
cnt++;
DFS_T(nod);
}
S.pop();
}
g<<cnt<<'\n';
for(int i=1; i<=cnt; i++)
{
for(auto& it:ctc[i])
g<<it<<" ";
g<<'\n';
}
return 0;
}