import java.util.*; public class SparseGraph { private List> neighbours; public SparseGraph(int n) { neighbours = new ArrayList<>(); for (int i = 0; i < n; i++) neighbours.add(new HashSet<>()); } public boolean hasEdge(int p, int q) { return neighbours.get(p).contains(q); } public Set edgesFrom(int p) { return neighbours.get(p); } public void addEdge(int p, int q) { neighbours.get(p).add(q); neighbours.get(q).add(p); } }