Pagini recente » Cod sursa (job #3284585) | Cod sursa (job #2812219) | Cod sursa (job #956680) | Cod sursa (job #551650) | Cod sursa (job #2724731)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
const int N=1e5+1;
const int M=2e5+1;
int ctc[N],nc=1;
vector<int> a[N],a_t[N];
//queue<int> stiva;
int stiva[N],vf;
bool viz[N],viz_t[N];
void dfs(int x)
{
viz[x]=1;
for(auto y: a[x])
{
if(!viz[y]) dfs(y);
}
stiva[++vf]=x;
//stiva.push(x);
}
void dfs_t(int x)
{
viz_t[x]=1;
ctc[x]=nc;
for(auto y: a_t[x])
{
if(!viz_t[y]) dfs_t(y);
}
}
int main()
{
int n,m,x,y,cnt=0;
in>>n>>m;
for(int i=1; i<=m; i++)
{
in>>x>>y;
a[x].push_back(y);
a_t[y].push_back(x);
}
for(int i=1; i<=n; i++)
{
if(viz[i]==0) dfs(i);
}
while(vf>0)
{
if(viz_t[stiva[vf]]==0)
{
dfs_t(stiva[vf]);
nc++;
}
vf--;
}
/*while(!stiva.empty())
{
if(viz_t[stiva.back()]==0)
{
dfs_t(stiva.back());
nc++;
}
stiva.pop();
}*/
nc--;
out<<nc<<"\n";
for(int i=1; i<=nc; i++)
{
for(int j=1; j<=n; j++)
{
if(ctc[j]==i) out<<j<<" ";
}
out<<"\n";
}
return 0;
}