-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSCgraph-h.cpp
104 lines (96 loc) · 1.83 KB
/
SCgraph-h.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
#include <iostream>
#include <vector>
using namespace std;
class graph{
public:
vector<int>* adjList;
int nv,ne;
graph(int n){
nv=n;
ne=0;
adjList=new vector<int>[nv+1];
}
void addEdge(int src,int dest){
adjList[src].push_back(dest);
}
//just for debugging process
void printGraph(){
for(int i=0;i<nv;i++){
cout<<i<<" : ";
for(int j=0;j<adjList[i].size();j++){
cout<<adjList[i][j]<<" ";
}
cout<<"\n";
}
}
void dfs(int src,bool visited[]){
visited[src]=true;
for(int i=0;i<adjList[src].size();i++){
int child=adjList[src][i];
if(!visited[child]){
dfs(child,visited);
}
}
}
bool dfs_helper(int src){
bool visited[nv+1]={};
dfs(src,visited);
for(int i=0;i<nv;i++){
if(!visited[i])
return false;
}
return true;
}
};
int main(){
int nv,ne;
cin>>nv>>ne;
graph g(nv);
while(ne--){
int a,b;
cin>>a>>b;
g.addEdge(a,b);
}
bool isConnected=g.dfs_helper(0);
//dfs helper tells if we were able to visit all the nodes from 0
if(isConnected==false){
cout<<"\nGraph is not connected\n";
}
else{
//we reverse all the edges
graph transposed(nv);
for(int i=0;i<nv;i++){
for(int j=0;j<g.adjList[i].size();i++){
int child=g.adjList[i][j];
transposed.addEdge(child,i);
}
}
//if we are here we are sure that we can visit all nodes from 0
//Now to check if all the nodes are able to visit 0, we reverse the edges
//(this is similar to applying dfs from each node if edges weren't reversed)
//and if now 0 is able to visit all the nodes, then it is strongly connected
//otherwise not
bool isStronglyConnected=transposed.dfs_helper(0);
if(isStronglyConnected){
cout<<"\nGraph is strongly connected\n";
}else{
cout<<"\nGraph is not strongly connected\n";
}
}
return 0;
}
/*
5 6
0 1
1 2
2 3
3 0
2 4
4 2
Output : YES
4 3
0 1
1 2
2 3
Output : NO
*/