Let's start our journey and drive through the land of Data Structures. But, to start, we first need to know whether it is possible for us to reach somewhere. Suppose you are at your starting city Mumbai and you want to plan a Road trip to a pleasant and peaceful destination. First, we will check that Is it possible to reach a certain destination from our starting point or not? Let's solve this problem.
Say there are
- First line : Two integers
$n$ and$m$ , where$n$ denotes the number of places of interest to visit and$m$ denotes the Number of available routes. For simplicity, we are numbering the places of interests as integers$1,2,.....n$ . - Next
$m$ lines : Each line has two integers$x$ and$y$ , representing that there exists a route connecting place$x$ to$y$ . All the routes available are one-way only.
Print :
-
YES
: If all Tourist spots are possible to travel from any other Touristspot -
NO
: Otherwise. Also, print the spots$p, q$ such that you are not able to travel between them
Notice that if one vertex can be reached by all other vertices and every other vertex can be reached from this vertex, then every vertex in this graph can reach all others.
To understand this statement, consider a boolean function YES
. If not, then there exists a pair
You can compute YES
, else you can print 1 x
if you are running DFS on x 1
otherwise
4 6
1 2
2 1
1 3
2 3
3 4
4 3
NO
3 1
Analyse the time complexity of your Algorithm that you have used yourself :)
- We were working on
checkPaths.cpp
but were not able to solve the problem. Can you help us by contributing to this code and solving this question? - Upload your code as
<your_GitHub_username>_checkPaths.cpp
in this directory