Pagini recente » Cod sursa (job #2130436) | Istoria paginii runda/marcel001/clasament | Cod sursa (job #2404399) | Cod sursa (job #1005373) | Cod sursa (job #2888961)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const int N=100001;
vector <int> graf[N+1],graf_transpus[N+1];
vector <int> ctc[N+1];
vector <int> stiva;
bitset <N+1> viz;
int n,m,nc;
void DFS(int x)
{
viz[x]=1;
for(auto y: graf[x])
{
if(!viz[y])
{
DFS(y);
}
}
stiva.push_back(x);
}
void DFS_Tare(int x)
{
ctc[nc].push_back(x);
viz[x]=1;
for(auto y:graf_transpus[x])
if(!viz[y])
{
DFS_Tare(y);
}
}
int main()
{
ifstream f("ctc.in");
ofstream g("ctc.out");
int x,y;
f>>n>>m;
for(int i=1; i<=m; i++)
{
f>>x>>y;
graf[x].push_back(y);
graf_transpus[y].push_back(x);
}
f.close();
for(int i=1; i<=n; i++)
if(!viz[i])
{
DFS(i);
}
viz.reset();
for(int i=(int)stiva.size()-1; i>=0; i--)
if(!viz[stiva[i]])
{
nc++;
DFS_Tare(stiva[i]);
}
g<<nc<<endl;
for(int i=1; i<=nc; i++)
{
for(auto x: ctc[i])
{
g<<x<<" ";
}
g<<endl;
}
g.close();
return 0;
}