Pagini recente » Cod sursa (job #539062) | Cod sursa (job #1713037) | Cod sursa (job #1151718) | Cod sursa (job #1502608) | Cod sursa (job #2790334)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
const int N=1e5+1;
int n,m,nr;
vector <int> a[N],b[N],sort_top,ctc[N];
bool vf[N];
void dfs_s(int x){
vf[x]=1;
for(auto j:a[x]){
if(!vf[j])
dfs_s(j);
}
sort_top.push_back(x);
}
void dfs_p(int x){
vf[x]=0;
for(auto j:b[x]){
if(vf[j])
dfs_p(j);
}
ctc[nr].push_back(x);
}
int main() {
f>>n>>m;
for(int i=1; i<=m; i++){
int x,y;
f>>x>>y;
a[x].push_back(y);
b[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(!vf[i])
dfs_s(i);
for(int i=n-1; i>=0; i--){
if(vf[sort_top[i]]){
nr++;
dfs_p(sort_top[i]);
}
}
g<<nr<<'\n';
for(int i=1; i<=nr; i++){
for(auto j:ctc[i])
g<<j<<" ";
g<<'\n';
}
return 0;
}