/**
* Definition for Directed graph.
* class DirectedGraphNode {
* int label;
* ArrayList<DirectedGraphNode> neighbors;
* DirectedGraphNode(int x) {
* label = x;
* neighbors = new ArrayList<DirectedGraphNode>();
* }
* };
*/
public class Solution {
/*
* @param graph: A list of Directed graph node
* @param s: the starting Directed graph node
* @param t: the terminal Directed graph node
* @return: a boolean value
*/
public boolean hasRoute(ArrayList<DirectedGraphNode> graph, DirectedGraphNode s, DirectedGraphNode t) {
if(s == t) return true;
// write your code here
HashSet<DirectedGraphNode> visited = new HashSet<DirectedGraphNode> ();
Queue<DirectedGraphNode> q = new LinkedList<>();
q.offer(s);
visited.add(s);
while(!q.isEmpty()) {
DirectedGraphNode node = q.poll();
for(int i = 0; i < node.neighbors.size(); i++) {
if(visited.contains(node.neighbors.get(i))) {
continue;
}
if(node.neighbors.get(i) == t) return true;
visited.add(node.neighbors.get(i));
q.offer(node.neighbors.get(i));
}
}
return false;
}
}