Add graph/tarjan.cc
Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
parent
fa4fe9522f
commit
c48002df20
|
@ -0,0 +1,44 @@
|
|||
namespace tarjan {
|
||||
vector<int> scc(const vector<vector<int>>& ch) {
|
||||
int n = ch.size();
|
||||
int cnt = 0, scn = 0;
|
||||
vector<int> dfn(n), low(n), vis(n), st;
|
||||
vector<int> br(n);
|
||||
auto tarjan = [&] (auto tarjan, int v) -> void {
|
||||
dfn[v]=low[v]=++cnt;
|
||||
st.push_back(v);
|
||||
vis[v]=1;
|
||||
for(const auto&u:ch[v])
|
||||
if(!dfn[u]) tarjan(tarjan, u),low[v]=min(low[v],low[u]);
|
||||
else if(vis[u])low[v]=min(low[v],dfn[u]);
|
||||
if(dfn[v]==low[v]){
|
||||
++scn;
|
||||
int u;
|
||||
do u=st.back(), st.pop_back(),vis[u]=0,br[u]=scn; while(u!=v);
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!dfn[i]) {
|
||||
tarjan(tarjan, i);
|
||||
}
|
||||
}
|
||||
return br;
|
||||
}
|
||||
|
||||
vector<vector<int>> build_scc(const vector<vector<int>>& ch) {
|
||||
int n = ch.size();
|
||||
auto br = scc(ch);
|
||||
int cnt = *max_element(br.begin(), br.end());
|
||||
vector<unordered_set<int, safe_hash>> rb(cnt + 1);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (auto&& u : ch[i]) {
|
||||
rb[br[i]].emplace(br[u]);
|
||||
}
|
||||
}
|
||||
vector<vector<int>> res(cnt + 1);
|
||||
for (int i = 1; i <= cnt; ++i) {
|
||||
res[i] = vector<int>(rb[i].begin(), rb[i].end());
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue