Pagini recente » Cod sursa (job #390031) | Cod sursa (job #159769) | Cod sursa (job #1961052) | Cod sursa (job #1280329) | Cod sursa (job #2195714)
#include <bits/stdc++.h>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m, x, y, vf, cnt, st[100100];
vector <int> v[100100], vv[100100], sol[100100];
bitset <100100> viz;
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(; cnt; cnt--){
out << '\n';
for(auto i : sol[cnt])
out << i << ' ';
}
return 0;
}