You are given an undirected graph with n vertices labelled 0..n-1 and m edges. After the edge list, a single query gives a source vertex s and a destination vertex d.
Decide whether there exists a path (a sequence of edges) that connects s and d. A vertex is always connected to itself.
Line 1: two integers n and m.
The next m lines each contain two integers u v, an undirected edge between u and v.
The final line contains two integers s d, the query.
Print YES if s and d are connected, otherwise print NO.
Example 1
Input
4 2 0 1 2 3 0 3
Expected
NO
Explanation
0 connects to 1, and 2 connects to 3, but the two pairs are separate, so 0 cannot reach 3. Answer is NO.
Example 2
Input
4 3 0 1 1 2 2 3 0 3
Expected
YES
Explanation
The chain 0-1-2-3 links every vertex, so 0 reaches 3. Answer is YES.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →