Pagini recente » Cod sursa (job #1395643) | Cod sursa (job #1305108) | Cod sursa (job #2267527) | Cod sursa (job #2577931) | Cod sursa (job #2439543)
#include <bits/stdc++.h>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
const int N = 1e5+5;
vector<int> v[N],vt[N],ans[N];
stack<int> ord;
bool viz[N];
int k;
void dfp(int x)
{
viz[x] = 1;
for (auto it: v[x])
if (!viz[it])
dfp(it);
ord.push(x);
}
void dfm(int x)
{
viz[x] = 1;
ans[k].push_back(x);
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);
}
dfp(1);
memset(viz,0,sizeof(viz));
while (!ord.empty())
{
int now = ord.top();
ord.pop();
if (!viz[now])
{
k++;
dfm(now);
}
}
out << k << "\n";
for (int i = 1; i<=k; i++)
{
for (auto it: ans[i])
out << it << " ";
out << "\n";
}
}