Pagini recente » Cod sursa (job #279612) | Cod sursa (job #835129) | Cod sursa (job #2124736) | Cod sursa (job #2099290) | Cod sursa (job #3214066)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, st[100005], top, nr;
bitset <100005> viz;
vector <int> a[100005], t[100005];
vector <int> cc[100005];
void Dfs1(int x)
{
viz[x] = 1;
for (auto w : a[x])
if (viz[w] == 0)
Dfs1(w);
st[++top] = x;
}
void Dfs2(int x)
{
viz[x] = 0;
for (auto w : t[x])
if (viz[w] == 1)
Dfs2(w);
cc[nr].push_back(x);
}
int main()
{
int i, x, y;
fin >> n >> m;
for (i = 1; i <= m; i++)
{
fin >> x >> y;
a[x].push_back(y);
t[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (viz[i] == 0)
Dfs1(i);
for (i = n; i >= 1; i--)
if (viz[st[i]] == 1)
{
nr++;
Dfs2(st[i]);
}
fout << nr << "\n";
for (i = 1; i <= nr; i++)
{
for (auto w : cc[i])
fout << w << " ";
fout << "\n";
}
return 0;
}