import java.util.*; public class Main { public static boolean[] visited = new boolean[5001]; public static HashMap<Integer, ArrayList<Integer>> edges= new HashM...