Pagini recente » Cod sursa (job #2694870) | Cod sursa (job #2419201) | Cod sursa (job #3181729) | Cod sursa (job #2496517) | Cod sursa (job #2569466)
#include <bits/stdc++.h>
#define Nmax 100005
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
int N, M, ans;
vector<int> G[Nmax], GT[Nmax], nodes, ctc[Nmax];
bool vis[Nmax];
void DFS1(int node) {
vis[node] = 1;
for (auto it: G[node])
if (!vis[it]) DFS1(it);
nodes.push_back(node);
}
void DFS2(int node) {
vis[node] = 1;
ctc[ans].push_back(node);
for (auto it: GT[node])
if (!vis[it]) DFS2(it);
}
int main()
{
f >> N >> M;
for (int i = 1; i <= M; ++i) {
int x, y;
f >> x >> y;
G[x].push_back(y);
GT[y].push_back(x);
}
for (int i = 1; i <= N; ++i)
if (!vis[i]) DFS1(i);
reverse(nodes.begin(), nodes.end());
memset(vis, 0, sizeof(vis));
for (auto it : nodes)
if (!vis[it]) ++ans, DFS2(it);
g << ans << '\n';
for (int i = 1; i <= ans; ++i) {
for (auto it : ctc[i])
g << it << " ";
g << '\n';
}
return 0;
}