Pagini recente » Cod sursa (job #876114) | Cod sursa (job #495744) | Cod sursa (job #242827) | Cod sursa (job #161270) | Cod sursa (job #2183386)
#include <bits/stdc++.h>
#define N 100100
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m, x, y, vf, st[N], cnt;
vector <int> v[N], vv[N], sol[N];
bool viz[N];
void predfs(int from){
viz[from] = 1;
for(auto to : v[from])
if(!viz[to])
predfs(to);
st[++vf] = from;
}
void dfs(int from){
viz[from] = 0;
sol[cnt].push_back(from);
for(auto to : vv[from])
if(viz[to])
dfs(to);
}
int main(){
in >> n >> m;
while(m--){
in >> x >> y;
v[x].push_back(y);
vv[y].push_back(x);
}
for(int i = 1; i <= n; i++)
if(!viz[i])
predfs(i);
for(; vf; vf--)
if(viz[st[vf]]){
++cnt;
dfs(st[vf]);
}
out << cnt;
for(int i = 1; i <= cnt; i++){
out << '\n';
for(auto j : sol[i])
out << j << ' ';
}
return 0;
}