Pagini recente » Cod sursa (job #1327371) | Cod sursa (job #2314336) | Cod sursa (job #2116834) | Cod sursa (job #1011599) | Cod sursa (job #3300787)
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <fstream>
#include <cstring>
using namespace std;
#define fast_io ios::sync_with_stdio(0); cin.tie(0); do{}while(0)
typedef long long ll;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
const int MAXN = 1e5 + 5;
int n, m;
vector<int> graph[MAXN];
vector<int> path;
int discovery[MAXN];
int link[MAXN];
bool inStack[MAXN];
vector<vector<int>> ctcs;
int t;
void ReadData() {
fin >> n >> m;
int a, b;
for (int i = 0; i < m; i++) {
fin >> a >> b;
graph[a].push_back(b);
}
}
void DFS(int node) {
t++;
path.push_back(node);
discovery[node] = t;
link[node] = t;
inStack[node] = true;
for (int newNode : graph[node]) {
if (!discovery[newNode]) {
DFS(newNode);
link[node] = min(link[newNode], link[node]);
} else if (inStack[newNode]) {
link[node] = min(link[node], discovery[newNode]);
}
}
if (discovery[node] == link[node]) {
vector<int> ctc;
while (!path.empty()) {
int v = path.back();
path.pop_back();
inStack[v] = false;
ctc.push_back(v);
if (v == node) {
break;
}
}
ctcs.push_back(ctc);
}
}
void Solve() {
for (int i = 1; i <= n; i++) {
if (discovery[i]) {
continue;
}
DFS(i);
}
fout << ctcs.size() << '\n';
for (auto c : ctcs) {
for (int x : c) {
fout << x << ' ';
}
fout << '\n';
}
}
int main() {
ReadData();
Solve();
return 0;
}