-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStrongly_Connected_Component.cpp
121 lines (112 loc) · 2.49 KB
/
Strongly_Connected_Component.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/// Author: Mohd. Raihan Chowdhury
/// International Islamic University Chittagong
/// Strongly Connected Component
/// Source: http://www.shafaetsplanet.com/planetcoding/?p=2531
/// Video Source: https://www.youtube.com/watch?v=ko2STYDN-vM&t=507s
/// Happy Coding :)
#include <bits/stdc++.h>
using namespace std;
const int mx = 100005;
vector <int> g[mx]; /// main graph
vector <int> r[mx]; /// reverse graph
vector <int> top;
bool visit[mx];
int comp[mx];
int inDegree[mx];
vector <int> p[mx];
void init (int n)
{
for(int i = 0; i < n; i++)
{
g[i].clear();
r[i].clear();
p[i].clear();
}
memset(visit, false, sizeof visit);
top.clear();
memset(inDegree , 0, sizeof inDegree);
memset(comp, 0, sizeof comp);
}
void dfs1(int u)
{
visit[u] = true;
for(int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if(visit[v] == false)
dfs1(v);
}
top.push_back(u);
}
void dfs2(int u , int c)
{
visit[u] = true;
comp[u] = c;
for(int i = 0; i < r[u].size(); i++)
{
int v = r[u][i];
if(visit[v] == false)
{
dfs2(v, c);
}
}
}
int main()
{
int n , e;
while(cin >> n >> e)
{
init(n);
for(int i = 0; i < e; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
r[b].push_back(a); /// reverse
}
for(int i = 0; i < n; i++)
{
if(visit[i] == false)
dfs1(i);
}
for(int i = 0 ; i <= n; i++)
{
visit[i] = 0;
}
int cId = 0;
/// component
for(int i = top.size() - 1; i >= 0; i--)
{
if(visit[top[i]] == false)
{
dfs2(top[i] , ++cId);
}
}
/// indegree calculate
for(int i = 0; i < n; i++)
{
for(int j = 0; j < g[i].size(); j++)
{
if(comp[i] != comp[g[i][j]])
{
inDegree[comp[g[i][j]]]++;
}
}
}
/// printing component
for(int i = 0; i < n; i++)
{
p[comp[i]].push_back(i);
}
for(int i = 1; i <= cId; i++)
{
printf("{ ");
for(int j = 0; j < p[i].size(); j++)
{
printf("%d ", p[i][j]);
}
printf("}\n");
printf("And its indegree = %d\n", inDegree[i]);
}
}
}