#include <iostream>
#include <vector>
#include <stack>
#define MAXN 10001
using namespace std;
//ifstream fin("ctc.in");
//ofstream fout("ctc.out");
class Kosaraju {
// constructor-ul clasei
public:
Kosaraju(int N, int M): nodes( N ),
edges( M ),
comp( 0 ),
Graph(2 * N + 1),
Graph_reverse(2 * N + 1),
isVisited(2 * N + 1),
Results(2 * N + 1){}
void addEdges(int x, int y) {
Graph[x].push_back(y);
Graph_reverse[y].push_back(x);
}
void DFS(int node) {
isVisited[node] = 1;
for (vector<int>::iterator it = Graph[node].begin(); it != Graph[node].end(); ++it) {
if (!isVisited[*it]) {
DFS(*it);
}
}
Stack.push(node);
}
void DFS_Reverse(int node) {
isVisited[node] = 1;
Results[comp].push_back(node);
for (vector<int>::iterator it = Graph_reverse[node].begin(); it != Graph_reverse[node].end(); ++it) {
if (!isVisited[*it]) {
DFS_Reverse(*it);
}
}
Stack.push(node);
}
// kosaraju in action
void solve() {
int vertex = 0;
for (int i = 1; i <= nodes; i++)
if (!isVisited[i]) {
DFS(i);
}
for (int v = 1; v <= nodes + 1; v++) isVisited[v] = 0;
while (!Stack.empty()) {
vertex = Stack.top();
Stack.pop();
if (!isVisited[vertex]) {
comp++;
DFS_Reverse(vertex);
}
}
}
void writeData() {
// Scriem numarul de componente tare conexe
cout << comp << '\n';
for (int i = 1; i <= comp; i++) {
for (vector<int>::iterator it = Results[i].begin(); it != Results[i].end(); ++it) {
cout << *it << " ";
}
cout << '\n';
}
}
private:
int nodes, edges, comp;
vector<vector<int>> Graph;
vector<vector<int>> Graph_reverse;
vector<vector<int>> Results;
vector<int> isVisited;
stack<int> Stack;
}; // end class Kosaraju
int main() {
int n, // nr de noduri
m, // nr de arce
x, // doua noduri
y; //
cin >> n >> m;
Kosaraju ob(n,m);
for (int i = 1; i <= m; i++) {
cin >> x >> y;
ob.addEdges(x,y);
}
//fin.close();
ob.solve();
ob.writeData();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RhY2s+CiNkZWZpbmUgTUFYTiAxMDAwMQp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy9pZnN0cmVhbSBmaW4oImN0Yy5pbiIpOwovL29mc3RyZWFtIGZvdXQoImN0Yy5vdXQiKTsKCmNsYXNzIEtvc2FyYWp1IHsKCiAgLy8gY29uc3RydWN0b3ItdWwgY2xhc2VpCiAgcHVibGljOgogICAgS29zYXJhanUoaW50IE4sIGludCBNKTogbm9kZXMoIE4gKSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVkZ2VzKCBNICksCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb21wKCAwICksCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBHcmFwaCgyICogTiArIDEpLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgR3JhcGhfcmV2ZXJzZSgyICogTiArIDEpLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgaXNWaXNpdGVkKDIgKiBOICsgMSksCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBSZXN1bHRzKDIgKiBOICsgMSl7fQoKICB2b2lkIGFkZEVkZ2VzKGludCB4LCBpbnQgeSkgewogICAgR3JhcGhbeF0ucHVzaF9iYWNrKHkpOwogICAgR3JhcGhfcmV2ZXJzZVt5XS5wdXNoX2JhY2soeCk7CiAgfQoKICB2b2lkIERGUyhpbnQgbm9kZSkgewogICAgaXNWaXNpdGVkW25vZGVdID0gMTsKICAgIGZvciAodmVjdG9yPGludD46Oml0ZXJhdG9yIGl0ID0gR3JhcGhbbm9kZV0uYmVnaW4oKTsgaXQgIT0gR3JhcGhbbm9kZV0uZW5kKCk7ICsraXQpIHsKICAgICAgaWYgKCFpc1Zpc2l0ZWRbKml0XSkgewogICAgICAgIERGUygqaXQpOwogICAgICB9CiAgICB9CiAgICBTdGFjay5wdXNoKG5vZGUpOwogIH0KCiAgdm9pZCBERlNfUmV2ZXJzZShpbnQgbm9kZSkgewogICAgaXNWaXNpdGVkW25vZ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