Artifact Content
Not logged in

Artifact e473bfcfb4e00b1656b29acf3d1a2c169c48361e



//-------------------------------------------------------------
// Strongly Connected Component of a Directed Graph
//   O(E)
//
// Verified by
//   - SRM 499 Div1 LV3
//
// Using "Spagetthi Source"'s one pass algorithm
//-------------------------------------------------------------

template<typename T>
class IdGen
{
	map<T, int> v2id_;
	vector<T>   id2v_;
public:
	int v2id(const T& v) {
		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
		return v2id_[v];
	}
	const T& id2v(int i) const { return id2v_[i]; }
	int size() const { return id2v_.size(); }
};

template<typename Vert>
class SCC
{
	IdGen<Vert> idgen;
	vector< vector<int> > G;

public:
	void addVert( Vert s_ )
	{
		int s = idgen.v2id(s_);
		if( s >= G.size() ) G.resize(s+1);
	}

	void addEdge( Vert s_, Vert t_ )
	{
		int s = idgen.v2id(s_), t = idgen.v2id(t_);
		if( max(s,t) >= G.size() ) G.resize(max(s,t)+1);
		G[s].push_back(t);
	}

	void scc()
	{
		int N = G.size();
		no = 0; // whatever small enough
		dfs_no.assign(N, 0);
		dfs_lo.assign(N, 0);
		pending = stack<int>();
		scc_id.assign(N, -1);
		scc_list.clear();
		scc_children.clear();
		for(int v=0; v<N; ++v)
			dfs(v);
	}

	vector<int> scc_id; // which scc does the vert belong to?
	vector< vector<int> > scc_list;     // list of nodes in the scc
	vector< vector<int> > scc_children; // forest relationship of sccs

private:
	int no;
	vector<int> dfs_no; // 1-origin dfs-visit ID
	vector<int> dfs_lo; // the least ID in the vert's scc
	stack<int> pending; // current unclassigied verts

	void dfs(int v)
	{
		// visit v if not yet
		if( dfs_no[v] )
			return;
		dfs_no[v] = dfs_lo[v] = ++no;
		pending.push(v);

		// visit children
		for(int i=0; i<G[v].size(); ++i) {
			int u = G[v][i];
			dfs( u );
			if( scc_id[u] == -1 )
				dfs_lo[v] = min( dfs_lo[v], dfs_lo[u] );
		}

		// when v is the representative of scc
		if( dfs_no[v] == dfs_lo[v] ) {
			vector<int> scc;
			for(;;) {
				int w = pending.top(); pending.pop();
				scc.push_back(w);
				scc_id[w] = scc_list.size();
				if( w == v ) break;
			}
			scc_list.push_back(scc);

			set<int> children;
			for(int j=0; j<scc.size(); ++j)
				for(int i=0; i<G[scc[j]].size(); ++i)
					children.insert( scc_id[G[scc[j]][i]] );
			children.erase(scc_id[v]);
			scc_children.push_back( vector<int>(children.begin(), children.end()) );
		}
	}
};