|
| 1 | +--- |
| 2 | +title: 3067.在带权树网络中统计可连接服务器对数目 |
| 3 | +date: 2024-06-04 22:55:48 |
| 4 | +tags: [题解, LeetCode, 中等, 树, 深度优先搜索, 数组] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】3067.在带权树网络中统计可连接服务器对数目:枚举根 |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/](https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/) |
| 10 | + |
| 11 | +<p>给你一棵无根带权树,树中总共有 <code>n</code> 个节点,分别表示 <code>n</code> 个服务器,服务器从 <code>0</code> 到 <code>n - 1</code> 编号。同时给你一个数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code> 表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条双向边,边的权值为 <code>weight<sub>i</sub></code> 。再给你一个整数 <code>signalSpeed</code> 。</p> |
| 12 | + |
| 13 | +<p>如果两个服务器 <code>a</code> ,<code>b</code> 和 <code>c</code> 满足以下条件,那么我们称服务器 <code>a</code> 和 <code>b</code> 是通过服务器 <code>c</code> <strong>可连接的</strong> :</p> |
| 14 | + |
| 15 | +<ul> |
| 16 | + <li><code>a < b</code> ,<code>a != c</code> 且 <code>b != c</code> 。</li> |
| 17 | + <li>从 <code>c</code> 到 <code>a</code> 的距离是可以被 <code>signalSpeed</code> 整除的。</li> |
| 18 | + <li>从 <code>c</code> 到 <code>b</code> 的距离是可以被 <code>signalSpeed</code> 整除的。</li> |
| 19 | + <li>从 <code>c</code> 到 <code>b</code> 的路径与从 <code>c</code> 到 <code>a</code> 的路径没有任何公共边。</li> |
| 20 | +</ul> |
| 21 | + |
| 22 | +<p>请你返回一个长度为 <code>n</code> 的整数数组 <code>count</code> ,其中 <code>count[i]</code> 表示通过服务器 <code>i</code> <strong>可连接</strong> 的服务器对的 <strong>数目</strong> 。</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p><b>示例 1:</b></p> |
| 27 | + |
| 28 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/21/example22.png" style="width: 438px; height: 243px; padding: 10px; background: #fff; border-radius: .5rem;" /></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<b>输入:</b>edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1 |
| 32 | +<b>输出:</b>[0,4,6,6,4,0] |
| 33 | +<b>解释:</b>由于 signalSpeed 等于 1 ,count[c] 等于所有从 c 开始且没有公共边的路径对数目。 |
| 34 | +在输入图中,count[c] 等于服务器 c 左边服务器数目乘以右边服务器数目。 |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong class="example">示例 2:</strong></p> |
| 38 | + |
| 39 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2024/01/21/example11.png" style="width: 495px; height: 484px; padding: 10px; background: #fff; border-radius: .5rem;" /></p> |
| 40 | + |
| 41 | +<pre> |
| 42 | +<b>输入:</b>edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3 |
| 43 | +<b>输出:</b>[2,0,0,0,0,0,2] |
| 44 | +<b>解释:</b>通过服务器 0 ,有 2 个可连接服务器对(4, 5) 和 (4, 6) 。 |
| 45 | +通过服务器 6 ,有 2 个可连接服务器对 (4, 5) 和 (0, 5) 。 |
| 46 | +所有服务器对都必须通过服务器 0 或 6 才可连接,所以其他服务器对应的可连接服务器对数目都为 0 。 |
| 47 | +</pre> |
| 48 | + |
| 49 | +<p> </p> |
| 50 | + |
| 51 | +<p><strong>提示:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>2 <= n <= 1000</code></li> |
| 55 | + <li><code>edges.length == n - 1</code></li> |
| 56 | + <li><code>edges[i].length == 3</code></li> |
| 57 | + <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li> |
| 58 | + <li><code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code><!-- notionvc: a2623897-1bb1-4c07-84b6-917ffdcd83ec --></li> |
| 59 | + <li><code>1 <= weight<sub>i</sub> <= 10<sup>6</sup></code></li> |
| 60 | + <li><code>1 <= signalSpeed <= 10<sup>6</sup></code></li> |
| 61 | + <li>输入保证 <code>edges</code> 构成一棵合法的树。</li> |
| 62 | +</ul> |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## 解题方法:枚举根 |
| 67 | + |
| 68 | +枚举每个节点作为```c```,以```c```为根,求每个“子树”中有多少节点离```c```的距离是```signalSpeed```的总个数(可以通过DFS求出)。 |
| 69 | + |
| 70 | +``` |
| 71 | + c |
| 72 | + / \ |
| 73 | + * *** |
| 74 | + ** |
| 75 | +``` |
| 76 | + |
| 77 | +假设所有子树中符合要求的节点数分别为```[4, 5, 8]```,则以```c```为根的总对数为```4 * 5 + 4 * 8 + 5 * 8```(两两相乘)。 |
| 78 | + |
| 79 | ++ 时间复杂度$O(n^2)$ |
| 80 | ++ 空间复杂度$O(n)$ |
| 81 | + |
| 82 | +### AC代码 |
| 83 | + |
| 84 | +#### C++ |
| 85 | + |
| 86 | +```cpp |
| 87 | +class Solution { |
| 88 | +private: |
| 89 | + vector<vector<pair<int, int>>> graph; |
| 90 | + int signalSpeed; |
| 91 | + |
| 92 | + int dfs(int from, int to, int cntDistance) { |
| 93 | + int ans = cntDistance % signalSpeed == 0; |
| 94 | + for (auto [nextNode, nextDistance] : graph[to]) { |
| 95 | + if (nextNode == from) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + ans += dfs(to, nextNode, cntDistance + nextDistance); |
| 99 | + } |
| 100 | + return ans; |
| 101 | + } |
| 102 | +public: |
| 103 | + vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) { |
| 104 | + // init |
| 105 | + graph.resize(edges.size() + 1); |
| 106 | + this->signalSpeed = signalSpeed; |
| 107 | + for (vector<int>& edge : edges) { |
| 108 | + graph[edge[0]].push_back({edge[1], edge[2]}); |
| 109 | + graph[edge[1]].push_back({edge[0], edge[2]}); |
| 110 | + } |
| 111 | + // calculate |
| 112 | + vector<int> ans(edges.size() + 1); |
| 113 | + for (int c = 0; c < ans.size(); c++) { |
| 114 | + vector<int> ab; // c为根的每个边上有多少ab节点 |
| 115 | + for (auto [to, distance] : graph[c]) { |
| 116 | + ab.push_back(dfs(c, to, distance)); |
| 117 | + } |
| 118 | + for (int i = 0; i < ab.size(); i++) { |
| 119 | + for (int j = i + 1; j < ab.size(); j++) { |
| 120 | + ans[c] += ab[i] * ab[j]; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return ans; |
| 125 | + } |
| 126 | +}; |
| 127 | +``` |
| 128 | +
|
| 129 | +#### Python |
| 130 | +
|
| 131 | +```python |
| 132 | +# from typing import List |
| 133 | +
|
| 134 | +class Solution: |
| 135 | + def dfs(self, from_: int, to: int, cntDistance: int) -> int: |
| 136 | + ans = 0 if cntDistance % self.signalSpeed else 1 |
| 137 | + for nextNode, nextDistance in self.graph[to]: |
| 138 | + if nextNode == from_: |
| 139 | + continue |
| 140 | + ans += self.dfs(to, nextNode, cntDistance + nextDistance) |
| 141 | + return ans |
| 142 | +
|
| 143 | + def countPairsOfConnectableServers(self, edges: List[List[int]], signalSpeed: int) -> List[int]: |
| 144 | + # init |
| 145 | + self.signalSpeed = signalSpeed |
| 146 | + graph = [[] for _ in range(len(edges) + 1)] |
| 147 | + for x, y, d in edges: |
| 148 | + graph[x].append((y, d)) |
| 149 | + graph[y].append((x, d)) |
| 150 | + self.graph = graph |
| 151 | + # calculate |
| 152 | + ans = [0] * (len(edges) + 1) |
| 153 | + for c in range(len(ans)): |
| 154 | + ab = [] |
| 155 | + for to, distance in graph[c]: |
| 156 | + ab.append(self.dfs(c, to, distance)) |
| 157 | + for i in range(len(ab)): |
| 158 | + for j in range(i + 1, len(ab)): |
| 159 | + ans[c] += ab[i] * ab[j] |
| 160 | + return ans |
| 161 | +
|
| 162 | +``` |
| 163 | + |
| 164 | +> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/06/04/LeetCode%203067.%E5%9C%A8%E5%B8%A6%E6%9D%83%E6%A0%91%E7%BD%91%E7%BB%9C%E4%B8%AD%E7%BB%9F%E8%AE%A1%E5%8F%AF%E8%BF%9E%E6%8E%A5%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%AF%B9%E6%95%B0%E7%9B%AE/)哦~ |
| 165 | +> |
| 166 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/139456087](https://letmefly.blog.csdn.net/article/details/139456087) |
0 commit comments