Pagini recente » Cod sursa (job #1026065) | Cod sursa (job #671052) | Cod sursa (job #1968317) | Cod sursa (job #724674) | Cod sursa (job #3317006)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector<int> v[100001], vt[100001];
stack<int> s;
vector<int> viz;
vector<int> nrctc[100001];
int n, m, cnt;
void DFS(int x){
viz[x] = 1;
for(auto &y:v[x])
if(!viz[y])
DFS(y);
s.push(x);
}
void DFST(int x){
viz[x] = 1;
nrctc[cnt].push_back(x);
for(auto &y:vt[x])
if(!viz[y])
DFST(y);
}
int main()
{
fin>>n>>m;
viz.resize(n+1);
while(m--){
int x,y;
fin>>x>>y;
v[x].push_back(y);
vt[y].push_back(x);
}
for(int i=1; i<=n; ++i)
if(!viz[i])
DFS(i);
viz.assign(n+1, 0);
while(!s.empty()){
int i = s.top();
s.pop();
if(!viz[i]){
cnt++;
DFST(i);
}
}
fout<<cnt<<endl;
for(int i=1; i<=cnt; ++i){
for(auto &e:nrctc[i])
fout<<e<<" ";
fout<<endl;
}
return 0;
}