Problem Description
Given an undirected weighted graph with n vertices (labeled 0 to n – 1) and an array of edges (each given as [u, v, w]), a walk on the graph is a sequence of vertices and edges (vertices and edges may repeat). The cost of a walk is defined as the bitwise AND (using the & operator) of all of its edge weights. You are also given several queries; each query is a pair [s, t] asking: what is the minimum possible cost that can be obtained by some walk from vertex s to vertex t? If no such walk exists, return –1.
Key Insights
- A walk may include cycles and extra detours. This means that even if there exists a “direct” s–t path that avoids low bits, we can always detour (go off the direct route and come back) to “force” additional bits to be cleared (changed to 0).
- For any bit position, if there is at least one edge in the connected component that is missing that bit (i.e. has a 0 in that bit), then it is possible to design an s–t walk that passes through that edge; therefore, that bit can be “cleared” in the final AND.
- Conversely, if in the entire connected component every edge has a certain bit set then no walk between any two vertices in that component can clear that bit.
- Hence, for any query (s, t): if s and t are not in the same connected component (using all edges), the answer is –1; otherwise, the minimum achievable cost equals the bitwise AND over all edge weights in that connected component.
Space and Time Complexity
Time Complexity: O(m · α(n)) for union–find processing of the m edges plus an O(m) pass to compute the per–component AND; O(q) for answering q queries. Space Complexity: O(n) for DSU arrays and O(m) to store the edges.
Solution
We use a disjoint–set union (DSU) structure to find the full connected components of the graph (ignoring weights). Once the components are identified, observe that because we can take arbitrarily long walks (repeating vertices and edges as needed), if there exists even one edge in the component whose weight does not have a given bit set then we may “detour” to include it and force that bit to 0 in our overall AND. Therefore, for any component the best (i.e. minimum) cost achievable on any s–t walk is simply the bitwise AND over the weights of all edges present in that component. (If every edge in the component has a certain bit set, then that bit will be forced to 1; otherwise it can be dropped to 0 by including a detour through an edge that is missing that bit.) The query answer is then: • If s and t are not in the same full component, answer –1. • Otherwise, answer = (AND of all edge weights in the component containing s).
The “gotcha” is to realize that because walks are allowed to be arbitrarily long, you are never forced to “pick” only one particular s–t path. Instead, you can always add a detour to lower the AND value, and as a result the minimum cost is completely determined by the properties of the overall connected component.