Pagini recente » Cod sursa (job #764189) | Rating Andrei (qfl1ck32) | Cod sursa (job #444098) | Cod sursa (job #458370) | Cod sursa (job #2103003)
#include <bits/stdc++.h>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
int n, m, x, y, cnt, vf, stk[100100];
vector <int> v[100100], vv[100100];
bool viz[100100];
vector <int> sol[100100];
void predfs(int from){
viz[from] = 1;
for(auto to : v[from])
if(!viz[to])
predfs(to);
stk[++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[stk[vf]]){
cnt++;
dfs(stk[vf]);
}
out << cnt << '\n';
for(int i = 1; i <= cnt; i++, out << '\n')
for(auto nod : sol[i])
out << nod << ' ';
return 0;
}