Pagini recente » Cod sursa (job #333668) | Rating Dragos Cirstea (SirDragos13) | Rating Domil Serban (Domil) | Profil Alatir | Cod sursa (job #998220)
Cod sursa(job #998220)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <utility>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <iomanip>
#include <set>
#include <map>
#include <cassert>
#include <ctime>
#include <list>
#include <iomanip>
using namespace std;
string file = "scc";
ifstream cin( (file + ".in").c_str() );
ofstream cout( (file + ".out").c_str() );
const int MAXN = 100005;
const int oo = (1<<31)-1;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
struct ClassComp {
inline bool operator () (const int &a, const int &b) const {
return a > b;
}
};
Graph G;
int N, M, Level[MAXN], LowLink[MAXN], deep;
set<set<int> > SCC;
stack<int> Stack;
bitset<MAXN> inStack;
inline void Tarjan(int Node) {
Level[Node] = LowLink[Node] = ++ deep;
Stack.push(Node);
inStack[Node] = true;
for(It it = G[Node].begin(), fin = G[Node].end() ; it != fin ; ++ it) {
if(!Level[*it]) {
Tarjan(*it);
Get_min(LowLink[Node], LowLink[*it]);
}
else if(inStack[*it])
Get_min(LowLink[Node], LowLink[*it]);
}
if(LowLink[Node] == Level[Node]) {
set<int>actSCC;
int node;
do {
actSCC.insert(node = Stack.top());
Stack.pop();
inStack[node] = false;
}while(node != Node);
SCC.insert(actSCC);
}
}
int main() {
cin >> N >> M;
for(int i = 1 ; i <= M ; ++ i) {
int x, y;
cin >> x >> y;
G[x].push_back(y);
}
for(int i = 1 ; i <= N ; ++ i)
if(!Level[i])
Tarjan(i);
cout << SCC.size() << '\n';
for(set<set<int> > :: iterator it = SCC.begin(), fin = SCC.end() ; it != fin ; ++ it, cout << '\n')
for(set<int>::iterator itt = it->begin() , finn = it->end() ; itt != finn ; ++ itt)
cout << *itt << ' ';
cin.close();
cout.close();
return 0;
}