Pagini recente » Cod sursa (job #642739) | Cod sursa (job #2489051) | Cod sursa (job #2613477) | Cod sursa (job #328181) | Cod sursa (job #1690259)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
const int nmax = 1e5+5;
vector <int> g[nmax], gt[nmax], ctc[nmax];
bitset <nmax> viz;
int st[nmax], top, nr;
void sort_top(int dad) {
viz[dad]=true;
for(auto son : g[dad])
if(viz[son]==false) sort_top(son);
st[++top]=dad;
}
void dfs(int dad) {
viz[dad]=true;
ctc[nr].push_back(dad);
for(auto son : gt[dad])
if(viz[son]==false) dfs(son);
}
int main() {
ios_base::sync_with_stdio(false);
int n, m, i, x, y;
fin >> n >> m;
for(i=1; i<=m; i++) {
fin >> x >> y;
g[x].push_back(y);
gt[y].push_back(x);
}
for(i=1; i<=n; i++)
if(viz[i]==false) sort_top(i);
viz=0;
for(i=top; i; i--) {
if(viz[st[i]]==false) {
nr++;
dfs(st[i]);
}
}
fout << nr << "\n";
for(i=1; i<=nr; i++) {
for(auto it : ctc[i])
fout << it << " ";
fout << '\n';
}
fin.close();
fout.close();
return 0;
}