-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisConnected.cpp
More file actions
72 lines (64 loc) · 1.29 KB
/
isConnected.cpp
File metadata and controls
72 lines (64 loc) · 1.29 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
#include <iostream>
using namespace std;
void isConnected(int **matrix,int V,int start,bool*visited)
{
visited[start]=true;
for(int i=0;i<V;i++)
{
if(matrix[i][start]==1 &&!visited[i])
{
visited[i]=true;
isConnected(matrix,V,i,visited);
}
}
}
int main() {
int V, E;
cin >> V >> E;
/*
Write Your Code Here
Complete the Rest of the Program
You have to take input and print the output yourself
*/
int **matrix=new int*[V];
for(int i=0;i<V;i++)
{
matrix[i]=new int[V];
for(int j=0;j<V;j++)
{
matrix[i][j]=0;
}
}
for(int i=0;i<E;i++)
{
int s,f;
cin>>s>>f;
matrix[s][f]=matrix[f][s]=1;
}
bool * visited=new bool[V];
int flag=0;
for(int i=0;i<V;i++)
{
visited[i]=false;
}
isConnected(matrix,V,0,visited);
for(int i=0;i<V;i++)
{
if(visited[i]==false)
{
flag=1;
cout<<"false";
break;
}
}
if(flag==0){
cout<<"true";
}
delete[]visited;
for(int i=0;i<V;i++)
{
delete[]matrix[i];
}
delete[]matrix;
return 0;
}