#C1662. Flight Connection Problem
Flight Connection Problem
Flight Connection Problem
You are given a directed graph representing cities and one‐way flights between them. The graph has \(n\) cities numbered from 0 to \(n-1\) and a list of \(m\) flights. Each flight is represented as a pair \((u, v)\) which indicates there is a flight from city \(u\) to city \(v\). Given two cities: a start city and an end city, your task is to determine if it is possible to travel from the start to the end using one or more available flights.
In mathematical terms, given a graph \(G=(V, E)\) where \(V = \{0,1,2, \ldots, n-1\}\) and \(E\) is the set of directed edges, determine whether there exists a path from vertex start to vertex end.
The recommended approach is to perform a breadth-first search (BFS) or depth-first search (DFS) starting from the start city.
inputFormat
The input is read from standard input. The first line contains an integer \(T\) representing the number of test cases. Each test case starts with a line containing four integers \(n\), \(m\), \(start\) and \(end\), where \(n\) is the number of cities, \(m\) is the number of flights, \(start\) is the starting city, and \(end\) is the destination city. This is followed by \(m\) lines, each containing two integers \(u\) and \(v\) representing a flight from city \(u\) to city \(v\).
Example:
1 3 3 0 2 0 1 1 2 2 0
outputFormat
For each test case, output a single line containing YES
if it is possible to travel from the start city to the end city, or NO
otherwise.
Example:
YES## sample
1
3 3 0 2
0 1
1 2
2 0
YES
</p>