#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
vector<int> ke[MAXN], keNguoc[MAXN];
bool daTham[MAXN];
stack<int> thuTuDinh;
vector<vector<int>> thanhPhanLienThongManh;
int soDinh, soCanh;
void dfs1(int u) {
daTham[u] = true;
for (int v : ke[u]) {
if (!daTham[v]) dfs1(v);
}
thuTuDinh.push(u);
}
void dfs2(int u, vector<int>& thanhPhan) {
daTham[u] = true;
thanhPhan.push_back(u);
for (int v : keNguoc[u]) {
if (!daTham[v]) dfs2(v, thanhPhan);
}
}
void timThanhPhanLienThongManh() {
fill(daTham, daTham + soDinh + 1, false);
for (int i = 1; i <= soDinh; i++) {
if (!daTham[i]) dfs1(i);
}
fill(daTham, daTham + soDinh + 1, false);
while (!thuTuDinh.empty()) {
int u = thuTuDinh.top(); thuTuDinh.pop();
if (!daTham[u]) {
vector<int> thanhPhan;
dfs2(u, thanhPhan);
sort(thanhPhan.begin(), thanhPhan.end());
thanhPhanLienThongManh.push_back(thanhPhan);
}
}
sort(thanhPhanLienThongManh.begin(), thanhPhanLienThongManh.end());
}
int main() {
cin >> soDinh >> soCanh;
for (int i = 0; i < soCanh; i++) {
int u, v;
cin >> u >> v;
ke[u].push_back(v);
keNguoc[v].push_back(u);
}
timThanhPhanLienThongManh();
cout << thanhPhanLienThongManh.size() << "\n";
for (const auto& tp : thanhPhanLienThongManh) {
for (int dinh : tp) cout << dinh << " ";
cout << "\n";
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTUFYTiA9IDEwNTsKdmVjdG9yPGludD4ga2VbTUFYTl0sIGtlTmd1b2NbTUFYTl07CmJvb2wgZGFUaGFtW01BWE5dOwpzdGFjazxpbnQ+IHRodVR1RGluaDsKdmVjdG9yPHZlY3RvcjxpbnQ+PiB0aGFuaFBoYW5MaWVuVGhvbmdNYW5oOwppbnQgc29EaW5oLCBzb0Nhbmg7Cgp2b2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bmhQaGFuTGllblRob25nTWFuaC5iZWdpbigpLCB0aGFuaFBoYW5MaWVuVGhvbmdNYW5oLmVuZCgpKTsKfQoKaW50IG1haW4oKSB7CiAgICBjaW4gPj4gc29EaW5oID4+IHNvQ2FuaDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc29DYW5oOyBpKyspIHsKICAgICAgICBpbnQgdSwgdjsKICAgICAgICBjaW4gPj4gdSA+PiB2OwogICAgICAgIGtlW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICBrZU5ndW9jW3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIAogICAgdGltVGhhbmhQaGFuTGllblRob25nTWFuaCgpOwogICAgCiAgICBjb3V0IDw8IHRoYW5oUGhhbkxpZW5UaG9uZ01hbmguc2l6ZSgpIDw8ICJcbiI7CiAgICBmb3IgKGNvbnN0IGF1dG8mIHRwIDogdGhhbmhQaGFuTGllblRob25nTWFuaCkgewogICAgICAgIGZvciAoaW50IGRpbmggOiB0cCkgY291dCA8PCBkaW5oIDw8ICIgIjsKICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICB9CiAgICByZXR1cm4gMDsKfQo=