Pagini recente » Cod sursa (job #2823738) | Cod sursa (job #2344872) | Cod sursa (job #2948356) | Cod sursa (job #2285807) | Cod sursa (job #2218043)
#include <bits/stdc++.h>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
const int NMAX = 1e5+5;
vector<int> v[NMAX], vt[NMAX], sol[NMAX];
bool viz[NMAX];
stack <int> comp;
int nr;
void dfp(int x)
{
viz[x] = 1;
for (auto it: v[x])
if (!viz[it])
dfp(it);
comp.push(x);
}
void dfm(int x)
{
sol[nr].push_back(x);
viz[x] = 1;
for (auto it: vt[x])
if (!viz[it])
dfm(it);
}
int main()
{
int n,m;
in >> n >> m;
for (int i = 1; i<=m; i++)
{
int x,y;
in >> x >> y;
v[x].push_back(y);
vt[y].push_back(x);
}
for (int i = 1; i<=n; i++)
if (!viz[i])
dfp(i);
memset(viz,0,sizeof(viz));
while (!comp.empty())
{
int now = comp.top();
if (!viz[now])
{
nr++;
dfm(now);
}
comp.pop();
}
out << nr << "\n";
for (int i = 1; i<=nr; i++)
{
for (auto it: sol[i])
out << it << " ";
out << "\n";
}
}