Pagini recente » Cod sursa (job #2891604) | Cod sursa (job #2913923) | Cod sursa (job #1984174) | Cod sursa (job #1218903) | Cod sursa (job #3240378)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, x, y, nr;
vector <int> G[100005], GT[100005], ctc[100005];
stack <int> q;
bitset <100005> viz;
void dfs1(int nod)
{
viz[nod] = 1;
for (auto next : G[nod])
if (viz[next] == 0)
dfs1(next);
q.push(nod);
}
void dfs2(int nod)
{
viz[nod] = 1;
ctc[nr].push_back(nod);
for (auto next : GT[nod])
if (viz[next] == 0)
dfs2(next);
}
int main()
{
int i, j, nod;
fin >> n >> m;
while (m--)
{
fin >> x >> y;
G[x].push_back(y);
GT[y].push_back(x);
}
for (i = 1 ; i <= n ; i++)
if (viz[i] == 0)
dfs1(i);
viz.reset();
while (!q.empty())
{
nod = q.top();
q.pop();
if (viz[nod] == 0)
{
++nr;
dfs2(nod);
}
}
fout << nr << "\n";
for (i = 1 ; i <= nr ; i++)
{
for (auto next : ctc[i])
fout << next << " ";
fout << "\n";
}
}