D - Minimum XOR Path Editorial by en_translator
First, let us consider how many simple paths starting from vertex \(1\) there are. If the graph is a complete graph (with \(M=N(N-1)/2\)), the sequence of the vertices in the simple path \((v_1,v_2,\ldots,v_k)\) satisfies \(v_1=1\) and \(v_i\neq v_j(i\neq j)\); there are \({}_{N-1}P_0+{}_{N-1}P_1+\ldots+{}_{N-1}P_{N-1}\) such paths (where \({}_nP_r\) is the number of ways to choose \(k\) items out of \(n\) distinct items). This value is \(\Theta((N-1)!)\), which is about \(10^6\) for \(N=10\). Thus, one can enumerate all possible simple paths.
To enumerate simple paths, DFS (Depth-First Search) is efficient. The answer can be found by performing DFS while maintaining (the last visited vertex, the array to memorize whether each vertex is visited, the total XOR of the labels on the edges already passed through). See also the sample code below.
N, M = map(int, input().split())
G = [[] * N for i in range(N)] # Adjacency list. Stores (vertex, edge label) pairs
for i in range(M):
u, v, w = map(int, input().split())
u -= 1
v -= 1
G[u].append((v, w))
G[v].append((u, w))
ans = 1 << 60
visited = [False] * N # Stores whether each vertex is visited
def dfs(v, xor):
global ans
visited[v] = True # Mark vertex v as visited
# If the current vertex is (N-1), update the answer
if v == N - 1:
ans = min(ans, xor)
for u, w in G[v]:
# If vertex u is unvisited, advance to vertex u
if not visited[u]:
dfs(u, xor ^ w)
visited[v] = False # Mark as unvisited
dfs(0, 0)
print(ans)
posted:
last update: