Pagini recente » Cod sursa (job #1787580) | Cod sursa (job #439002) | Cod sursa (job #1061055) | Cod sursa (job #2391541) | Cod sursa (job #3349490)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
const int dim = 100002;
bool viz[dim];
vector<int> a[dim], t[dim], finished, rez[dim];
void dfs(int x)
{
viz[x] = 1;
for (auto y : a[x])
if (!viz[y])
dfs(y);
finished.push_back(x);
}
void dfs_t(int x, int c)
{
rez[c].push_back(x);
viz[x] = 1;
for (auto y : t[x])
if (!viz[y])
dfs_t(y, c);
}
int main()
{
int n, m, x, y, nrc = 0;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
a[x].push_back(y);
t[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (!viz[i])
dfs(i);
reverse(finished.begin(), finished.end());
for (int i = 1; i <= n; i++)
viz[i] = 0;
for (auto x : finished)
if (!viz[x])
{
nrc++;
dfs_t(x,nrc);
}
fout << nrc << '\n';
for(int i {1}; i <= nrc; ++i) {
for(int x : rez[i]) {
fout << x << " ";
}
fout << '\n';
}
return 0;
}