-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathhaspath.cpp
124 lines (99 loc) · 1.85 KB
/
haspath.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Name: Gourav Singh
https://www.linkedin.com/in/gouravsingh2580/
*/
/*
Given an undirected graph G(V, E) and two vertices v1 and v2(as integers), check if there exists any path between them or not. Print true or false.
V is the number of vertices present in graph G and vertices are numbered from 0 to V-1.
E is the number of edges present in graph G.
Input Format :
Line 1: Two Integers V and E (separated by space)
Next E lines : Two integers a and b, denoting that there exists an edge between vertex a and vertex b (separated by space)
Line (E+2) : Two integers v1 and v2 (separated by space)
Output Format :
true or false
Constraints :
2 <= V <= 1000
1 <= E <= 1000
0 <= v1, v2 <= V-1
Sample Input 1 :
4 4
0 1
0 3
1 2
2 3
1 3
Sample Output 1 :
true
Sample Input 2 :
6 3
5 3
0 1
3 4
0 3
Sample Output 2 :
false
*/
#include <bits/stdc++.h>
using namespace std;
bool hasPath(int** edges, int n, int a, int b, int* visited){
//Base Case
if (a==b)
{
return 1;
}
int result = 0;
for (int i = 0; i < n; ++i)
{
if (edges[a][i] == 1 && visited[i] == 0)
{
visited[i] = 1;
result = hasPath(edges, n, i, b, visited);
if (result==1)
{
return 1;
}
}
}
// for (int i = 0; i < n; ++i)
// {
// cout << visited[i] << ' ';
// }
return result;
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
int n, e;
cin>>n>>e;
int** edges = new int*[n];
for (int i = 0; i < n; ++i)
{
edges[i] = new int[n];
for (int j = 0; j < n; ++j)
{
edges[i][j] = 0;
}
}
int* visited = new int[n];
for (int i = 0; i < n; ++i)
{
visited[i] = 0;
}
for (int i = 0; i < e; ++i)
{
int a, b;
cin>>a>>b;
edges[a][b] = 1;
edges[b][a] = 1;
}
int c, d;
cin>>c>>d;
if(hasPath(edges, n, c, d, visited)){
cout << "true" << '\n';
}else{
cout << "false" << '\n';
}
return 0 ;
}