Given a directed graph, two specified nodes, and the complete list of edges, determine whether you can travel from the start node to the end node following the directed edges.
You are provided with:
n – the total number of nodes (labeled from 0 to n-1).m – the number of directed edges.u v meaning a directed edge from node u to node v.s and an end node t.Return true if a directed path exists from s to t; otherwise return false.
Input:
n = 5
m = 5
edges:
0 1
0 2
1 3
2 3
3 4
s = 0
t = 4
Output:
true
Explanation: We can follow edges 0 → 2 → 3 → 4 (or 0 → 1 → 3 → 4), so a path from 0 to 4 exists.
[0, n-1].Solve the problem with the following method:
def has_path(n: int, edges: list[list[int]], s: int, t: int) -> bool:
pass
Your implementation should aim for time efficient enough to handle the upper bounds.