Pagini recente » Cod sursa (job #1114465) | Cod sursa (job #357184) | Cod sursa (job #1082954) | Cod sursa (job #2136245) | Cod sursa (job #2338799)
#include <fstream>
#include <iostream>
#include <vector>
#define nmax 100005
using namespace std;
//ifstream fin("date.in");
ifstream fin("ctc.in");
ofstream fout("ctc.out");
vector <int> G1[nmax], G2[nmax];
int n, m, x, y, viz2[nmax], viz1[nmax * 2], st[nmax], k = 1, nr_el, j = 1;
void read(){
fin >> n >> m;
for(int i = 1; i <= m; ++i){
fin >> x >> y;
G1[x].push_back(y);
G2[y].push_back(x);
}
}
void DFS1(int nod){
viz1[nod] = 1;
for(int i = 0; i < G1[nod].size(); ++i)
if(!viz1[G1[nod][i]])
DFS1(G1[nod][i]);
st[k] = nod;
k++;
}
void DFS(int nod){
viz1[j] = nod;
j++;
viz2[nod] = 1;
for(int i = 0; i < G2[nod].size(); ++i)
if(!viz2[G2[nod][i]])
DFS(G2[nod][i]);
}
void kosaraju(){
for(int i = k - 1; i > 0; --i){
if(viz2[st[i]] == 0){
nr_el++;
DFS(st[i]);
}
viz1[j++] = -1;
}
fout << nr_el << endl;
int c = 0;
for(int i = 1; i < j; i++){
if(viz1[i] == -1 && c == 0){
fout << endl;
c = 1;
}
else if(viz1[i] != -1){
fout << viz1[i] << " ";
c = 0;
}
}
}
int main(){
read();
for(int i = 1; i <= n; ++i)
if(viz1[i] == 0)
DFS1(i);
kosaraju();
fin.close();
fout.close();
return 0;
}