Pagini recente » Cod sursa (job #2555892) | Cod sursa (job #2760820) | Cod sursa (job #2162895) | Cod sursa (job #728988) | Cod sursa (job #2756622)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
stack <int> S;
vector <int> Graf[100001], Graf_T[100001];
vector <int> ctc[100001];
int n, m, cnt, x, y, viz[100001];
void DFS_T(int nod_curent)
{
viz[nod_curent]=2;
ctc[cnt].push_back(nod_curent);
for(auto x:Graf_T[nod_curent])
{
if(viz[x]==1)
{
DFS_T(x);
}
}
}
void DFS(int nod_curent)
{
viz[nod_curent]=1;
for(auto x:Graf[nod_curent])
{
if(!viz[x])
{
DFS(x);
}
}
S.push(nod_curent);
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
fin>>x>>y;
Graf[x].push_back(y);
Graf_T[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();
}
fout<<cnt<<"\n";
for(int i=1; i<=cnt; i++)
{
for(auto x:ctc[i])
fout<<x<<" ";
fout<<"\n";
}
return 0;
}