-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHas_path.cpp
More file actions
85 lines (77 loc) · 1.48 KB
/
Has_path.cpp
File metadata and controls
85 lines (77 loc) · 1.48 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
#include <iostream>
#include<queue>
using namespace std;
bool f(int **matrix,int V,int path1,int path2,bool *visited)
{
if(matrix[path1][path2]==1)
{
return true;
}
else
{
queue<int>q;
q.push(path1);
visited[path1]=true;
while(!q.empty())
{
int current=q.front();
if(current==path2)
{
return true;
}
q.pop();
for(int i=0;i<V;i++)
{
if(matrix[i][current]==1 &&!visited[i])
{
q.push(i);
visited[i]=true;
}
}
}
return false;
}
}
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]=1;
matrix[f][s]=1;
}
int path1,path2;
cin>>path1>>path2;
bool *visited=new bool[V];
if(f(matrix,V,path1,path2,visited))
{
cout<<"true";
}
else
{
cout<<"false";
}
for(int i=0;i<V;i++)
{
delete[]matrix[i];
}
delete[]matrix;
delete[] visited;
return 0;
}