Pagini recente » Cod sursa (job #2607731) | Cod sursa (job #769580) | Cod sursa (job #1804535) | Cod sursa (job #775613) | Cod sursa (job #2147770)
#include <bits/stdc++.h>
#define nmax 100007
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, st[nmax], top, nrc;
vector <int> L[nmax];
vector <int> T[nmax];
vector <int> Sol[nmax];
bool viz[nmax];
void Citire()
{
int t, x, y;
fin >> n >> t;
while (t--)
{
fin >> x >> y;
L[x].push_back(y);
T[y].push_back(x);
}
}
void DFS(int nod)
{
viz[nod] = 1;
for (auto i : L[nod])
if (!viz[i])
DFS(i);
st[++top] = nod;
}
void DFS2(int nod)
{
viz[nod] = 1;
for (auto i : T[nod])
if (!viz[i])
DFS2(i);
Sol[nrc].push_back(nod);
}
void Rezolvare()
{
int i, nod;
for (i = 1; i <= n; i++)
if (!viz[i])
DFS(i);
for (i = 1; i <= n; i++)
viz[i] = 0;
while (top > 0)
{
nod = st[top--];
if (!viz[nod])
{
nrc++;
DFS2(nod);
}
}
}
void Afisare()
{
int i;
fout << nrc << "\n";
for (i = 1; i <= nrc; i++)
{
for (auto j : Sol[i])
fout << j << " ";
fout << "\n";
}
}
int main()
{
Citire();
Rezolvare();
Afisare();
fin.close();
fout.close();
return 0;
}