Pagini recente » Cod sursa (job #1665455) | Cod sursa (job #3039428) | Cod sursa (job #1326756) | Cod sursa (job #1660115) | Cod sursa (job #2439547)
#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);
}
for (int i = 1; i<=n; i++)
if (!viz[i])
dfp(i);
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";
}
}