Pagini recente » Cod sursa (job #2452956) | Cod sursa (job #2447413) | Cod sursa (job #462306) | Cod sursa (job #806681) | Cod sursa (job #2357162)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
int n, m, from,k, to, in=1, llk[100005], ix[100005], viz[100005];
vector<int>graph[100005];
vector<int>rez[100005];
stack<int>s;
void conexe(int ind)
{
ix[ind]=llk[ind]=in;
++in;
s.push(ind);
viz[ind]=1;
for (auto &v:graph[ind])
{
if (ix[v]==0)
{
conexe(v);
llk[ind]=min(llk[ind],llk[v]);
}
else if (viz[v]==1)
{
llk[ind]=min(llk[ind],llk[v]);
}
}
if (ix[ind]==llk[ind])
{
while (!s.empty())
{
int el=s.top();
s.pop();
rez[k].push_back(el);
viz[el]=0;
if (ix[el]==llk[el])
{
k++;
break;
}
}
}
}
int main()
{
f >> n >> m;
for (int i=1; i<=m; ++i)
{
f >> from >> to;
graph[from].push_back(to);
}
for (int i=1; i<=n;++i)
{
if (ix[i]==0)
{
conexe(i);
}
}
g << k << '\n';
for (int i=0; i<k; ++i)
{
for (auto &v:rez[i])
g << v <<' ';
g<<'\n';
}
return 0;
}