-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff28d4a
commit b5e7199
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
constexpr int MAXN = 100'00; | ||
|
||
int n, m; // 0-based | ||
vector<int> graph[MAXN]; | ||
/////////////////// | ||
int dfsn[MAXN], scc[MAXN]; | ||
stack<int> stk; | ||
vector<vector<int>> res; | ||
int ce; | ||
int dfs(int now) | ||
{ | ||
dfsn[now] = ce++; | ||
int ret = dfsn[now]; | ||
stk.push(now); | ||
for (int nxt: graph[now]) | ||
{ | ||
if (dfsn[nxt] == -1) ret = min(ret, dfs(nxt)); | ||
else if (scc[nxt] == -1) ret = min(ret, dfsn[nxt]); | ||
} | ||
if (ret == dfsn[now]) | ||
{ | ||
res.push_back(vector<int>()); | ||
int t; | ||
do | ||
{ | ||
t = stk.top(); stk.pop(); | ||
scc[t] = res.size() - 1; | ||
res.back().push_back(t); | ||
} while(t != now); | ||
} | ||
return ret; | ||
} | ||
void set_scc() | ||
{ | ||
memset(dfsn, -1, sizeof dfsn); | ||
memset(scc, -1, sizeof scc); | ||
res.clear(); | ||
ce = 0; | ||
for (int i=0;i<n;++i) | ||
if (dfsn[i] == -1) dfs(i); | ||
} |