Approach
Depth-first search
For Maximum XOR of Two Non-Overlapping Subtrees, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 92 lines of C++ from the credited upstream file 2479.cpp.
- The implementation visibly relies on sequence storage.
- 5 loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1struct TrieNode {2 vector<shared_ptr<TrieNode>> children;3 TrieNode() : children(2) {}4};5 6class BitTrie {7 public:8 BitTrie(int maxBit) : maxBit(maxBit) {}9 10 void insert(long num) {11 shared_ptr<TrieNode> node = root;12 for (int i = maxBit; i >= 0; --i) {13 const int bit = num >> i & 1;14 if (node->children[bit] == nullptr)15 node->children[bit] = make_shared<TrieNode>();16 node = node->children[bit];17 }18 }19 20 long getMaxXor(long num) {21 long maxXor = 0;22 shared_ptr<TrieNode> node = root;23 for (int i = maxBit; i >= 0; --i) {24 const int bit = num >> i & 1;25 const int toggleBit = bit ^ 1;26 if (node->children[toggleBit] != nullptr) {27 maxXor = maxXor | 1L << i;28 node = node->children[toggleBit];29 } else if (node->children[bit] != nullptr) {30 node = node->children[bit];31 } else { 32 return 0;33 }34 }35 return maxXor;36 }37 38 private:39 const int maxBit;40 shared_ptr<TrieNode> root = make_shared<TrieNode>();41};42 43class Solution {44 public:45 long long maxXor(int n, vector<vector<int>>& edges, vector<int>& values) {46 long ans = 0;47 vector<vector<int>> tree(n);48 vector<long> treeSums(n);49 50 for (const vector<int>& edge : edges) {51 const int u = edge[0];52 const int v = edge[1];53 tree[u].push_back(v);54 tree[v].push_back(u);55 }56 57 getTreeSum(tree, 0, -1, treeSums, values);58 const long maxSubTreeSum =59 *max_element(treeSums.begin() + 1, treeSums.end());60 const int maxBit = static_cast<int>(log2(maxSubTreeSum));61 62 dfs(tree, 0, -1, treeSums, BitTrie(maxBit), ans);63 return ans;64 }65 66 private:67 68 long getTreeSum(const vector<vector<int>>& tree, int u, int prev,69 vector<long>& treeSums, const vector<int>& values) {70 long treeSum = values[u];71 for (const int v : tree[u])72 if (v != prev)73 treeSum += getTreeSum(tree, v, u, treeSums, values);74 treeSums[u] = treeSum;75 return treeSum;76 }77 78 void dfs(const vector<vector<int>>& tree, int u, int prev,79 const vector<long>& treeSums, BitTrie&& bitTrie, long& ans) {80 for (const int v : tree[u]) {81 if (v == prev)82 continue;83 84 ans = max(ans, bitTrie.getMaxXor(treeSums[v]));85 86 dfs(tree, v, u, treeSums, std::move(bitTrie), ans);87 88 bitTrie.insert(treeSums[v]);89 }90 }91};92