美丽的神话 发表于 2024-6-11 19:19:02

LeetCode 3067.在带权树网络中统计可连接服务器对数目:罗列根

【LetMeFly】3067.在带权树网络中统计可连接服务器对数目:罗列根

力扣题目链接:https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/
给你一棵无根带权树,树中总共有 n 个节点,分别表示 n 个服务器,服务器从 0 到 n - 1 编号。同时给你一个数组 edges ,其中 edges =  表示节点 ai 和 bi 之间有一条双向边,边的权值为 weighti 。再给你一个整数 signalSpeed 。
如果两个服务器 a ,b 和 c 满足以下条件,那么我们称服务器 a 和 b 是通过服务器 c 可连接的 :


[*]a < b ,a != c 且 b != c 。
[*]从 c 到 a 的间隔是可以被 signalSpeed 整除的。
[*]从 c 到 b 的间隔是可以被 signalSpeed 整除的。
[*]从 c 到 b 的路径与从 c 到 a 的路径没有任何公共边。
请你返回一个长度为 n 的整数数组 count ,其中 count 表示通过服务器 i 可连接 的服务器对的 数目 。
 
示例 1:
https://img-blog.csdnimg.cn/img_convert/9d6a301c5954e4a5023bbeeca91b5da7.png
<b>输入:</b>edges = [,,,,], signalSpeed = 1
<b>输出:</b>
<b>解释:</b>由于 signalSpeed 等于 1 ,count 等于所有从 c 开始且没有公共边的路径对数目。
在输入图中,count 等于服务器 c 左边服务器数目乘以右边服务器数目。
示例 2:
https://img-blog.csdnimg.cn/img_convert/dffb90d13707ec56bb721edc6d60334b.png
<b>输入:</b>edges = [,,,,,], signalSpeed = 3
<b>输出:</b>
<b>解释:</b>通过服务器 0 ,有 2 个可连接服务器对(4, 5) 和 (4, 6) 。
通过服务器 6 ,有 2 个可连接服务器对 (4, 5) 和 (0, 5) 。
所有服务器对都必须通过服务器 0 或 6 才可连接,所以其他服务器对应的可连接服务器对数目都为 0 。
 
提示:


[*]2 <= n <= 1000
[*]edges.length == n - 1
[*]edges.length == 3
[*]0 <= ai, bi < n
[*]edges =
[*]1 <= weighti <= 106
[*]1 <= signalSpeed <= 106
[*]输入包管 edges 构成一棵正当的树。
解题方法:罗列根

罗列每个节点作为c,以c为根,求每个“子树”中有多少节点离c的间隔是signalSpeed的总个数(可以通过DFS求出)。
    c
   / \
   ****
**
假设全部子树中符合要求的节点数分别为,则以c为根的总对数为4 * 5 + 4 * 8 + 5 * 8(两两相乘)。


[*]时间复杂度                                        O                            (                                       n                               2                                    )                                  O(n^2)                     O(n2)
[*]空间复杂度                                        O                            (                            n                            )                                  O(n)                     O(n)
AC代码

C++

class Solution {
private:
    vector<vector<pair<int, int>>> graph;
    int signalSpeed;

    int dfs(int from, int to, int cntDistance) {
      int ans = cntDistance % signalSpeed == 0;
      for (auto : graph) {
            if (nextNode == from) {
                continue;
            }
            ans += dfs(to, nextNode, cntDistance + nextDistance);
      }
      return ans;
    }
public:
    vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {
      // init
      graph.resize(edges.size() + 1);
      this->signalSpeed = signalSpeed;
      for (vector<int>& edge : edges) {
            graph].push_back({edge, edge});
            graph].push_back({edge, edge});
      }
      // calculate
      vector<int> ans(edges.size() + 1);
      for (int c = 0; c < ans.size(); c++) {
            vector<int> ab;// c为根的每个边上有多少ab节点
            for (auto : graph) {
                ab.push_back(dfs(c, to, distance));
            }
            for (int i = 0; i < ab.size(); i++) {
                for (int j = i + 1; j < ab.size(); j++) {
                  ans += ab * ab;
                }
            }
      }
      return ans;
    }
};
Python

# from typing import List

class Solution:
    def dfs(self, from_: int, to: int, cntDistance: int) -> int:
      ans = 0 if cntDistance % self.signalSpeed else 1
      for nextNode, nextDistance in self.graph:
            if nextNode == from_:
                continue
            ans += self.dfs(to, nextNode, cntDistance + nextDistance)
      return ans

    def countPairsOfConnectableServers(self, edges: List], signalSpeed: int) -> List:
      # init
      self.signalSpeed = signalSpeed
      graph = [[] for _ in range(len(edges) + 1)]
      for x, y, d in edges:
            graph.append((y, d))
            graph.append((x, d))
      self.graph = graph
      # calculate
      ans = * (len(edges) + 1)
      for c in range(len(ans)):
            ab = []
            for to, distance in graph:
                ab.append(self.dfs(c, to, distance))
            for i in range(len(ab)):
                for j in range(i + 1, len(ab)):
                  ans += ab * ab
      return ans

   同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/139456087

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: LeetCode 3067.在带权树网络中统计可连接服务器对数目:罗列根