返回介绍

solution / 2500-2599 / 2509.Cycle Length Queries in a Tree / README_EN

发布于 2024-06-17 01:03:04 字数 5626 浏览 0 评论 0 收藏 0

2509. Cycle Length Queries in a Tree

中文文档

Description

You are given an integer n. There is a complete binary tree with 2n - 1 nodes. The root of that tree is the node with the value 1, and every node with a value val in the range [1, 2n - 1 - 1] has two children where:

  • The left node has the value 2 * val, and
  • The right node has the value 2 * val + 1.

You are also given a 2D integer array queries of length m, where queries[i] = [ai, bi]. For each query, solve the following problem:

  1. Add an edge between the nodes with values ai and bi.
  2. Find the length of the cycle in the graph.
  3. Remove the added edge between nodes with values ai and bi.

Note that:

  • A cycle is a path that starts and ends at the same node, and each edge in the path is visited only once.
  • The length of a cycle is the number of edges visited in the cycle.
  • There could be multiple edges between two nodes in the tree after adding the edge of the query.

Return _an array _answer_ of length _m_ where_ answer[i] _is the answer to the_ ith _query._

 

Example 1:

Input: n = 3, queries = [[5,3],[4,7],[2,3]]
Output: [4,5,3]
Explanation: The diagrams above show the tree of 23 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.
- After adding the edge between nodes 3 and 5, the graph contains a cycle of nodes [5,2,1,3]. Thus answer to the first query is 4. We delete the added edge and process the next query.
- After adding the edge between nodes 4 and 7, the graph contains a cycle of nodes [4,2,1,3,7]. Thus answer to the second query is 5. We delete the added edge and process the next query.
- After adding the edge between nodes 2 and 3, the graph contains a cycle of nodes [2,1,3]. Thus answer to the third query is 3. We delete the added edge.

Example 2:

Input: n = 2, queries = [[1,2]]
Output: [2]
Explanation: The diagram above shows the tree of 22 - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge.
- After adding the edge between nodes 1 and 2, the graph contains a cycle of nodes [2,1]. Thus answer for the first query is 2. We delete the added edge.

 

Constraints:

  • 2 <= n <= 30
  • m == queries.length
  • 1 <= m <= 105
  • queries[i].length == 2
  • 1 <= ai, bi <= 2n - 1
  • ai != bi

Solutions

Solution 1

class Solution:
  def cycleLengthQueries(self, n: int, queries: List[List[int]]) -> List[int]:
    ans = []
    for a, b in queries:
      t = 1
      while a != b:
        if a > b:
          a >>= 1
        else:
          b >>= 1
        t += 1
      ans.append(t)
    return ans
class Solution {
  public int[] cycleLengthQueries(int n, int[][] queries) {
    int m = queries.length;
    int[] ans = new int[m];
    for (int i = 0; i < m; ++i) {
      int a = queries[i][0], b = queries[i][1];
      int t = 1;
      while (a != b) {
        if (a > b) {
          a >>= 1;
        } else {
          b >>= 1;
        }
        ++t;
      }
      ans[i] = t;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> cycleLengthQueries(int n, vector<vector<int>>& queries) {
    vector<int> ans;
    for (auto& q : queries) {
      int a = q[0], b = q[1];
      int t = 1;
      while (a != b) {
        if (a > b) {
          a >>= 1;
        } else {
          b >>= 1;
        }
        ++t;
      }
      ans.emplace_back(t);
    }
    return ans;
  }
};
func cycleLengthQueries(n int, queries [][]int) []int {
  ans := []int{}
  for _, q := range queries {
    a, b := q[0], q[1]
    t := 1
    for a != b {
      if a > b {
        a >>= 1
      } else {
        b >>= 1
      }
      t++
    }
    ans = append(ans, t)
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文