D - XOR Shortest Walk Editorial by en_translator
This problem can be solved with a technique informally called vertex multiplexing.
Let \(G\) be the given graph. Consider the following graph \(G'\):
- \(G'\) is a graph with \(1024\) vertices \((1,0),(1,1),\dots,(1,1023), (2,0),(2,1),\ldots,(N,1023)\).
- \(G'\) has a directed edge from vertex \((x,s)\) to vertex \((y,s\ \mathrm{XOR}\ w)\) if and only if \(G\) has an edge from vertex \(x\) to vertex \(y\) with weight \(w\).
A vertex \((x,s)\) is reachable from vertex \((1,0)\) in \(G'\) if and only if there is an walk from vertex \(1\) to vertex \(x\) of weight \(s\) in \(G\).
Hence, it is sufficient to check if each of vertices \((N,0),(N,1),\ldots,(N,1023)\) is reachable from vertex \((1,0)\). Since the graph \(G'\) has \(1024N\) vertices and \(1024M\) edges, the problem can be solved in \(O((N+M)\max W_i)\) time by actually constructing the graph and performing BFS (Breadth-First Search) or DFS (Depth-First Search) on it.
posted:
last update: