Pagini recente » Cod sursa (job #2635785) | Cod sursa (job #2906304) | Cod sursa (job #729654) | Cod sursa (job #2349719) | Cod sursa (job #2725169)
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
#include <algorithm>
using namespace std;
ifstream in("ctc.in");
ofstream out("ctc.out");
const int N = 1e5 + 1;
int n, m, nc;
vector <int> a[N], a_t[N], afis[N];
bitset <N> viz;
stack <int> q;
void dfs(int x)
{
viz[x] = true;
for(auto y : a[x]){
if(!viz[y]) dfs(y);
}
q.push(x);
}
void dfs_t(int x)
{
viz[x] = true;
for(auto y : a_t[x]){
if(!viz[y])
{
afis[nc].push_back(y);
dfs_t(y);
}
}
}
int main()
{
in >> n >> m;
for(int i=0; i<m; i++){
int x, y;
in >> x >> y;
a[x].push_back(y);
a_t[y].push_back(x);
}
for(int i=1; i<=n; i++){
if(!viz[i]) dfs(i);
}
for(int i=1; i<=n; i++)
viz[i] = false;
while(!q.empty()){
int i = q.top();
q.pop();
if(!viz[i])
{
nc++;
afis[nc].push_back(i);
dfs_t(i);
}
}
out << nc << "\n";
for(int i=1; i<=nc; i++)
{
sort(afis[i].begin(),afis[i].end());
for(auto j : afis[i])
out << j << " ";
out << "\n";
}
return 0;
}