-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkosaraju.cpp
More file actions
92 lines (78 loc) · 1.53 KB
/
Copy pathkosaraju.cpp
File metadata and controls
92 lines (78 loc) · 1.53 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
#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
#define pb push_back
bool visited[1000001];
vector<int>a[1000001];
vector<int>transpose[1000001];
stack<int>st;
void dfs(int node) // function to sort nodes based on their finish time
{
visited[node]=true;
for(int child:a[node])
{
if(!visited[child])
dfs(child);
}
st.push(node);
}
void dfs2(int node) // dfs
{
visited[node]=true;
// cout<<node<<" ";
for(auto child:transpose[node])
{
if(!visited[child])
dfs2(child);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>n>>m;
int u,v;
for(int i=0;i<m;i++)
{
cin>>u>>v;
a[u].pb(v);
}
memset(visited,false,sizeof(visited));
for(int i=0;i<n;i++) // for building stack according to finish time
{
if(!visited[i])
dfs(i);
}
for(int i=0;i<n;i++) //creating transpose of graph(i.e. flipping edges)
{
for(auto it:a[i])
transpose[it].push_back(i);
}
memset(visited,false,sizeof(visited));
int cc=0;
while(!st.empty()) //visiting stack to find SCC
{
int ind=st.top();
st.pop();
if(!visited[ind])
{
cc++;
dfs2(ind);
}
}
cout<<cc<<endl;
for(int i=0;i<n;i++)
{
a[i].clear();
transpose[i].clear();
}
}
return 0;
}