-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.cpp
More file actions
65 lines (54 loc) · 1.54 KB
/
Copy pathimplementation.cpp
File metadata and controls
65 lines (54 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
vector<bool> visited;
void dfs(int source, vector<vector<int>>& adjList, vector<int>& order){
visited[source] = true;
for(auto neighbour: adjList[source]){
if(!visited[neighbour]){
dfs(neighbour,adjList,order);
}
}
order.push_back(source);
}
void stronglyConnectedComp(vector<vector<int>>& adjList,
vector<vector<int>>& adjCond,
vector<vector<int>>& components){
int n = adjList.size()-1;
adjCond.clear();
components.clear();
visited.assign(n+1,false);
vector<int> order;
for(int i=1; i<=n; i++){
if(!visited[i]){
dfs(i,adjList,order);
}
}
reverse(order.begin(), order.end());
vector<vector<int>> revAdjList(n+1);
for(int i=0; i<=n; i++){
for(auto neighbour: adjList[i]){
revAdjList[neighbour].push_back(i);
}
}
visited.assign(n+1,false);
vector<int> roots(n+1,0);
for(auto v: order){
if(!visited[v]){
vector<int> comp;
dfs(v, revAdjList, comp);
components.push_back(comp);
int root = *min_element(comp.begin(), comp.end());
for(auto u: comp){
roots[u] = root;
}
}
}
adjCond.assign(n+1,{});
for(int i=0; i<=n; i++){
for(auto neighbour: adjList[i]){
if(roots[i] != roots[neighbour]){
adjCond[roots[i]].push_back(roots[neighbour]);
}
}
}
}