-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathscc_kosaraju's_algorithm.cpp
More file actions
105 lines (93 loc) · 2.08 KB
/
scc_kosaraju's_algorithm.cpp
File metadata and controls
105 lines (93 loc) · 2.08 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Strongly Connected Components of a directed graph Kosaraju's Algorithm
Step 1 : Normal dfs and note the ending time of a node and sort it desendingly..
Step 2 : Make a graph with every edge reversed
Step 3 : Process the sorted nodes with revered graph and store the SCC
*/
//Classic
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define vp vector<pi>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define For(i,a,b) for(ll i=a;i<b;i++)
#define endl "\n"
#define debug(x) cout<<"AA Baju Smit------> "<<#x<<" -> "<<x<<endl
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define mint map<int,int>
#define mod 1000000007
#define ciN cin
////////////////////////////////////////////////////////////////
ll n;
vi adj[100001], Radj[100001];// Directed graph both tranposed and normal..
stack<int>order;// Order of ending of first dfs
vector<bool>vis(100001, 0);
vi scc;
void dfs(int start) {
vis[start] = 1;
for (auto x : adj[start]) {
if (!vis[x]) {
dfs(x);
}
}
order.push(start);
}
void Rdfs(int start) {
vis[start] = 1;
scc.pb(start);
for (auto x : Radj[start]) {
if (!vis[x]) {
Rdfs(x);
}
}
}
void jabru() {
cin >> n;
vi v(n); For(i, 0, n)cin >> v[i];
int e; cin >> e;
For(i, 0, e) {
int x, y; cin >> x >> y;
adj[x].pb(y);
Radj[y].pb(x);
}
// make normal and transpose graph...
for (int i = 1; i <= n; i++) {
if (!vis[i])dfs(i);
}
// step - 1&2 complete
vector<vi>compo; // This will contain vector of every component..
for (int i = 1; i <= n; i++)vis[i] = 0;// Clearing vis ...
// Performing Rdfs in the order of stact...
while (!order.empty()) {
int curr = order.top();
order.pop();
if (!vis[curr]) {
scc.clear();
Rdfs(curr);
if (scc.size() > 0)
compo.pb(scc);
}
}
// Now play around with the components.... :)
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll t;
t = 1;
// cin >> t;
// debug("START");
while (t--) {
jabru();
}
return 0;
}