-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStrongly Connected Components using Kosaraju's Algorithm.cpp
88 lines (78 loc) · 1.48 KB
/
Strongly Connected Components using Kosaraju's Algorithm.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
/*input
5 6
1 4
1 3
2 4
3 4
4 5
5 1
*/
// https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/tutorial/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define dd double
#define pb push_back
#define mp make_pair
const ll N = 5E5;
ll n, m;
vector<ll> graph[N], Tgraph[N];
ll vis[N];
stack<ll> st;
vector<vector<ll> > All_SCC;
void dfs(ll src)
{
vis[src] = 1;
for(auto node : graph[src])
if(vis[node] == 0)
dfs(node);
st.push(src);
}
void Tdfs(ll src, vector<ll> &scc)
{
vis[src] = 1;
scc.pb(src);
for(auto node : Tgraph[src])
if(vis[node] == 0)
Tdfs(node, scc);
}
void strongly_connected_components()
{
for(ll i = 0; i < n; i++) vis[i] = 0;
for(ll i = 0; i < n; i++)
if(vis[i] == 0)
dfs(i);
for(ll i = 0; i < n; i++) vis[i] = 0;
while(!st.empty())
{
ll top = st.top();
st.pop();
if(vis[top] == 1) continue;
else
{
vector<ll> scc;
Tdfs(top, scc);
All_SCC.pb(scc);
}
}
}
int main()
{
cin >> n >> m;
while(m--)
{
ll u, v; cin >> u >> v;
u--; v--;
graph[u].pb(v);
Tgraph[v].pb(u);
}
strongly_connected_components();
ll odd = 0, even = 0;
for(auto scc : All_SCC)
{
if(scc.size() & 1) odd += scc.size();
else even += scc.size();
}
cout << odd - even << endl;
return 0;
}