Is There a Path
Read a graph and two of its vertices, s and t, and print true if you can walk from s to t along the edges, false if you can't. A vertex can always reach itself.
How a graph is written
The first line has the number of vertices n and the number of edges m. Then come m lines, one per edge, each with the two vertices it joins. The vertices are numbered from 0 to n - 1, and an edge works both ways.
0 --- 1 --- 2 3 --- 4 5
This graph is written:
6 3
0 1
1 2
3 4
Vertex 5 has no edges, so it shows up only in the count 6.
The starter code already reads the graph into adj, where adj[u] lists the neighbours of u, then prints true or false by what path_exists returns. You write path_exists.
Input
- Line 1:
nandm. There are between 1 and 500 vertices and at most 2000 edges. - The next
mlines: the edges. No edge joins a vertex to itself, and no two edges join the same two vertices. - Last line:
sandt.
Output
true if there is a path from s to t, false otherwise.
Examples
Input:
6 3
0 1
1 2
3 4
0 2
Output:
true
From 0 to 1, then from 1 to 2.
Input:
6 3
0 1
1 2
3 4
2 4
Output:
false
No edge leaves the piece 0, 1, 2, so 4 can't be reached from 2.
Input:
6 3
0 1
1 2
3 4
5 5
Output:
true
5 has no edges, but it is already where it has to be.