Pagini recente » Cod sursa (job #2838531) | Cod sursa (job #632653) | Cod sursa (job #292441) | Cod sursa (job #1437991) | Cod sursa (job #2184288)
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
const int NMAX = 100000 + 5;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector <int> ans[NMAX];
vector <int> graph[NMAX], hparg[NMAX];
stack <int> s;
int n, m, ctc;
bool vis[NMAX];
void dfs(int node)
{
vis[node] = true;
for (int i: graph[node])
if (!vis[node])
dfs(i);
s.push(node);
}
void sfd(int node)
{
vis[node] = true;
ans[ctc].push_back(node);
for (int i: graph[node])
if (!vis[i])
sfd(i);
}
void write()
{
fout << ctc << '\n';
for (int i = 1; i <= ctc; ++i)
{
for (int j: ans[ctc])
fout << j << " ";
fout << '\n';
}
}
void read()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y;
graph[x].push_back(y);
hparg[y].push_back(x);
}
}
int main()
{
read();
for (int i = 1; i <= n; ++i)
if (!vis[i])
dfs(i);
memset(vis, 0, sizeof(vis));
while (!s.empty())
{
int x = s.top();
s.pop();
if (!vis[x])
{
++ctc;
sfd(x);
}
}
write();
return 0;
}