Pagini recente » Cod sursa (job #2185829) | Istoria paginii runda/antr2 | Cod sursa (job #2256506) | Cod sursa (job #1236450) | Cod sursa (job #2964517)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m;
int st[100005], top, k;
vector<int> a[100005], b[100005], sol[100005];
bitset<100005> viz;
void DFS1(int x)
{
viz[x] = 1;
for (auto e : a[x])
if (viz[e] == 0)
DFS1(e);
st[++top] = x;
}
void DFS2(int x)
{
viz[x] = 1;
for (auto e : b[x])
if (viz[e] == 0)
DFS2(e);
sol[k].push_back(x);
}
int main()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
a[x].push_back(y);
b[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (viz[i] == 0) DFS1(i);
viz.reset();
for (int i = n; i >= 1; i--)
if (viz[st[i]] == 0)
{
k++;
DFS2(st[i]);
}
fout << k << "\n";
for (int i = 1; i <= k; i++)
{
sort(sol[i].begin(), sol[i].end());
for (auto e : sol[i])
fout << e << " ";
fout << "\n";
}
fin.close();
fout.close();
return 0;
}