Pagini recente » Cod sursa (job #2283897) | Cod sursa (job #2283902) | Cod sursa (job #1780151) | Cod sursa (job #408388) | Cod sursa (job #2811969)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m, topo[100001], k, f[100001], nctc;
vector<int> g[100001], gt[100001], ctc[100001];
void read()
{
int x, y;
in >> n >> m;
while(m--)
in >> x >> y,
g[x].push_back(y),
gt[y].push_back(x);
}
void dfs(int nod)
{
f[nod] = 1;
for(auto i : g[nod])
if(!f[i])
dfs(i);
topo[++k] = nod;
}
void sort_topo()
{
for(int i = 1; i <= n; ++i)
if(!f[i])
dfs(i);
}
void dfs2(int nod)
{
f[nod] = 2;
ctc[nctc].push_back(nod);
for(auto i : gt[nod])
if(f[i] == 1)
dfs2(i);
}
void afis()
{
out << nctc << '\n';
while(nctc--)
{
for(auto i : ctc[nctc+1])
out << i << ' ';
out << '\n';
}
}
int main()
{
read();
sort_topo();
for(int i = k; i > 0; --i)
if(f[topo[i]] == 1)
nctc++,
dfs2(topo[i]);
afis();
return 0;
}