Pagini recente » Cod sursa (job #110519) | Cod sursa (job #1752518) | Cod sursa (job #1201274) | Cod sursa (job #3168251) | Cod sursa (job #2147765)
#include <bits/stdc++.h>
#define nmax 100007
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, viz[nmax], st[nmax], top;
vector <int> L[nmax];
vector <int> T[nmax];
vector <int> Sol[nmax];
int nrc;
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;
}