Skip to content

2322. Minimum Score After Removals on a Tree #1964

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the minimum score after removing two distinct edges from a tree, resulting in three connected components. The score is defined as the difference between the largest and smallest XOR values of the three components.

Approach

  1. Tree Representation: Represent the tree using an adjacency list for efficient traversal.
  2. DFS Traversal: Perform a depth-first search (DFS) to compute the following for each node:
    • In-time and Out-time: These help in identifying if a node lies within the subtree of another node.
    • Subtree XOR: The XOR of all node values in the subtree rooted at each node.
  3. Total XOR Calculation: Compute the XOR of all node values in the entire tree.
  4. Edge Removal Simulation:…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Jul 24, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jul 24, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants