Pagini recente » Cod sursa (job #243364) | Monitorul de evaluare | sim3 | Cod sursa (job #1482280) | Cod sursa (job #2090079)
#include <fstream>
#include <vector>
#include <map>
using namespace std;
ifstream cin("ctc.in");
ofstream cout("ctc.out");
int n, m;
vector<vector<int> > gr(100100);
vector<vector<int> > inv(100100);
vector<bool> used(100100);
vector<bool> USED(100100);
vector<vector<int> > ans;
vector<int> verif;
map<int, bool> go;
vector<int> comp;
void dfs(int root) {
used[root] = true;
go[root] = true;
verif.push_back(root);
for (auto &x : gr[root]) {
if (!used[x]) {
dfs(x);
}
}
}
void DFS(int root) {
USED[root] = true;
comp.push_back(root);
for (auto &x : inv[root]) {
if ((!USED[x]) && go[x]) {
USED[x] = true;
DFS(x);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
gr[a].push_back(b);
inv[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (used[i]) {
continue;
}
verif.clear();
go.clear();
dfs(i);
for (auto &x : verif){
if (!USED[x]){
comp.clear();
DFS(x);
ans.push_back(comp);
}
}
}
cout << ans.size() << '\n';
for (auto &x : ans) {
for (auto &y : x) {
cout << y << " ";
}
cout << '\n';
}
return 0;
}