Problem solution · C++

Number of Ways to Assign Edge Weights II

Number of Ways to Assign Edge Weights II: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Depth-first search
Source
walkccc LeetCode Solutions
Length
83 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Number of Ways to Assign Edge Weights II, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 83 lines of C++ from the credited upstream file 3559.cpp.
  • The implementation visibly relies on sequence storage.
  • 7 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.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeNumber of Ways to Assign Edge Weights II · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  vector<int> assignEdgeWeights(vector<vector<int>>& edges,                                vector<vector<int>>& queries) {    const int n = edges.size() + 1;    vector<int> ans;    vector<int> depth(n + 1);    vector<vector<int>> graph(n + 1);    vector<vector<int>> parent(kLog, vector<int>(n + 1, -1));     for (const vector<int>& edge : edges) {      const int u = edge[0];      const int v = edge[1];      graph[u].push_back(v);      graph[v].push_back(u);    }     dfs(1, -1, graph, parent, depth);     for (int k = 1; k < kLog; ++k)      for (int v = 1; v <= n; ++v)        if (parent[k - 1][v] != -1)          parent[k][v] = parent[k - 1][parent[k - 1][v]];     for (const vector<int>& query : queries) {      const int u = query[0];      const int v = query[1];      if (u == v) {        ans.push_back(0);      } else {        const int a = lca(u, v, parent, depth);        const int d = depth[u] + depth[v] - 2 * depth[a];        ans.push_back(modPow(2, d - 1));      }    }     return ans;  }  private:  static constexpr int kMod = 1'000'000'007;  static constexpr int kLog = 17;  // since 2^17 > 1e5   void dfs(int u, int p, const vector<vector<int>>& graph,           vector<vector<int>>& parent, vector<int>& depth) {    parent[0][u] = p;    for (const int v : graph[u])      if (v != p) {        depth[v] = depth[u] + 1;        dfs(v, u, graph, parent, depth);      }  }   int lca(int u, int v, const vector<vector<int>>& parent,          const vector<int>& depth) {    if (depth[u] < depth[v])      swap(u, v);     for (int k = kLog - 1; k >= 0; --k)      if (parent[k][u] != -1 && depth[parent[k][u]] >= depth[v])        u = parent[k][u];     if (u == v)      return u;     for (int k = kLog - 1; k >= 0; --k)      if (parent[k][u] != -1 && parent[k][u] != parent[k][v]) {        u = parent[k][u];        v = parent[k][v];      }     return parent[0][u];  }   long modPow(long x, long n) {    if (n == 0)      return 1;    if (n % 2 == 1)      return x * modPow(x % kMod, (n - 1)) % kMod;    return modPow(x * x % kMod, (n / 2)) % kMod;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗