I started thinking about this as a per-query BFS and immediately knew that wasn't going to scale.
Sort the queries by limit and process them in increasing order, incrementally adding edges with weight less than the current limit to a disjoint-set union (DSU) structure. For each query, check if the two nodes are in the same connected component after adding all eligible edges.
Pro tip: Mention that this offline approach reduces the time complexity to O(E log E + Q log Q) and is more efficient than running BFS/DFS for each query. Also, clarify that edges with weight equal to the limit are excluded because the condition is strictly less than.
Clarify that the graph is undirected and weighted, and that for each query we need to determine if a path exists using only edges with weight < limit. Note that the answer is a boolean array.
Use Disjoint Set Union (DSU) to efficiently maintain connected components as edges are added. DSU supports near-constant time union and find operations.
Sort queries by their limit in ascending order. Also sort edges by weight. Iterate through queries, adding all edges with weight < current limit to the DSU before answering the query.
For each query, after adding all eligible edges, check if the two nodes belong to the same component using DSU find. Store the boolean result.
Collect the boolean answers in the original query order (if queries were sorted, map back to original indices) and return the array.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.