Skip to content

Commit 50c924d

Browse files
committed
update: 添加问题“3067.在带权树网络中统计可连接服务器对数目”的代码和题解
1 parent 46ea6b0 commit 50c924d

10 files changed

+325
-8
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-03 14:56:17
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-03 14:58:12
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<int> distributeCandies(int candies, int num_people) {
14+
vector<int> ans(num_people);
15+
int num = 1, index = 0;
16+
while (candies) {
17+
int thisTime = min(num, candies);
18+
num++;
19+
candies -= thisTime;
20+
ans[index] += thisTime;
21+
index = (index + 1) % num_people;
22+
}
23+
return ans;
24+
}
25+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-04 21:34:22
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-04 21:44:11
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
vector<vector<pair<int, int>>> graph;
14+
int signalSpeed;
15+
16+
int dfs(int from, int to, int cntDistance) {
17+
int ans = cntDistance % signalSpeed == 0;
18+
for (auto [nextNode, nextDistance] : graph[to]) {
19+
if (nextNode == from) {
20+
continue;
21+
}
22+
ans += dfs(to, nextNode, cntDistance + nextDistance);
23+
}
24+
return ans;
25+
}
26+
public:
27+
vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {
28+
// init
29+
graph.resize(edges.size() + 1);
30+
this->signalSpeed = signalSpeed;
31+
for (vector<int>& edge : edges) {
32+
graph[edge[0]].push_back({edge[1], edge[2]});
33+
graph[edge[1]].push_back({edge[0], edge[2]});
34+
}
35+
// calculate
36+
vector<int> ans(edges.size() + 1);
37+
for (int c = 0; c < ans.size(); c++) {
38+
vector<int> ab; // c为根的每个边上有多少ab节点
39+
for (auto [to, distance] : graph[c]) {
40+
ab.push_back(dfs(c, to, distance));
41+
}
42+
for (int i = 0; i < ab.size(); i++) {
43+
for (int j = i + 1; j < ab.size(); j++) {
44+
ans[c] += ab[i] * ab[j];
45+
}
46+
}
47+
}
48+
return ans;
49+
}
50+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-06-04 21:55:34
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-06-04 22:16:45
6+
*/
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
class Solution {
13+
public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) {
14+
// init
15+
List<int[]>[] graph = new ArrayList[edges.length + 1];
16+
Arrays.setAll(graph, i -> new ArrayList<int[]>());
17+
for (int[] edge : edges) {
18+
graph[edge[0]].add(new int[]{edge[1], edge[2]});
19+
// HALF // 11点去洗澡,待会儿再背点单词,写不完了,先不写Java版本了。
20+
}
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-06-04 21:47:07
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-06-04 21:54:22
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def dfs(self, from_: int, to: int, cntDistance: int) -> int:
11+
ans = 0 if cntDistance % self.signalSpeed else 1
12+
for nextNode, nextDistance in self.graph[to]:
13+
if nextNode == from_:
14+
continue
15+
ans += self.dfs(to, nextNode, cntDistance + nextDistance)
16+
return ans
17+
18+
def countPairsOfConnectableServers(self, edges: List[List[int]], signalSpeed: int) -> List[int]:
19+
# init
20+
self.signalSpeed = signalSpeed
21+
graph = [[] for _ in range(len(edges) + 1)]
22+
for x, y, d in edges:
23+
graph[x].append((y, d))
24+
graph[y].append((x, d))
25+
self.graph = graph
26+
# calculate
27+
ans = [0] * (len(edges) + 1)
28+
for c in range(len(ans)):
29+
ab = []
30+
for to, distance in graph[c]:
31+
ab.append(self.dfs(c, to, distance))
32+
for i in range(len(ab)):
33+
for j in range(i + 1, len(ab)):
34+
ans[c] += ab[i] * ab[j]
35+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@
622622
|2960.统计已测试设备|简单|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/10/LeetCode%202960.%E7%BB%9F%E8%AE%A1%E5%B7%B2%E6%B5%8B%E8%AF%95%E8%AE%BE%E5%A4%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/138672383" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-tested-devices-after-test-operations/solutions/2772739/letmefly-2960tong-ji-yi-ce-shi-she-bei-k-59qt/" target="_blank">LeetCode题解</a>|
623623
|2965.找出缺失和重复的数字|简单|<a href="https://leetcode.cn/problems/find-missing-and-repeated-values/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/31/LeetCode%202965.%E6%89%BE%E5%87%BA%E7%BC%BA%E5%A4%B1%E5%92%8C%E9%87%8D%E5%A4%8D%E7%9A%84%E6%95%B0%E5%AD%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139357662" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-missing-and-repeated-values/solutions/2796819/letmefly-2965zhao-chu-que-shi-he-zhong-f-p2rn/" target="_blank">LeetCode题解</a>|
624624
|2982.找出出现至少三次的最长特殊子字符串II|中等|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/30/LeetCode%202982.%E6%89%BE%E5%87%BA%E5%87%BA%E7%8E%B0%E8%87%B3%E5%B0%91%E4%B8%89%E6%AC%A1%E7%9A%84%E6%9C%80%E9%95%BF%E7%89%B9%E6%AE%8A%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139334864" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-longest-special-substring-that-occurs-thrice-ii/solutions/2795996/letmefly-2982zhao-chu-chu-xian-zhi-shao-9ubg1/" target="_blank">LeetCode题解</a>|
625+
|3067.在带权树网络中统计可连接服务器对数目|中等|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/solutions/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139456087" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-pairs-of-connectable-servers-in-a-weighted-tree-network/solutions/2801125/letmefly-3067zai-dai-quan-shu-wang-luo-z-zf2j/" target="_blank">LeetCode题解</a>|
625626
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
626627
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
627628
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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>&nbsp;个节点,分别表示 <code>n</code>&nbsp;个服务器,服务器从 <code>0</code>&nbsp;到 <code>n - 1</code>&nbsp;编号。同时给你一个数组&nbsp;<code>edges</code>&nbsp;,其中&nbsp;<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, weight<sub>i</sub>]</code>&nbsp;表示节点&nbsp;<code>a<sub>i</sub></code> 和&nbsp;<code>b<sub>i</sub></code>&nbsp;之间有一条双向边,边的权值为&nbsp;<code>weight<sub>i</sub></code>&nbsp;。再给你一个整数&nbsp;<code>signalSpeed</code>&nbsp;。</p>
12+
13+
<p>如果两个服务器 <code>a</code>&nbsp;,<code>b</code>&nbsp;和 <code>c</code>&nbsp;满足以下条件,那么我们称服务器 <code>a</code>&nbsp;和 <code>b</code>&nbsp;是通过服务器 <code>c</code>&nbsp;<strong>可连接的</strong>&nbsp;:</p>
14+
15+
<ul>
16+
<li><code>a &lt; b</code>&nbsp;,<code>a != c</code> 且&nbsp;<code>b != c</code>&nbsp;。</li>
17+
<li>从&nbsp;<code>c</code>&nbsp;到&nbsp;<code>a</code>&nbsp;的距离是可以被&nbsp;<code>signalSpeed</code>&nbsp;整除的。</li>
18+
<li>从&nbsp;<code>c</code>&nbsp;到&nbsp;<code>b</code>&nbsp;的距离是可以被&nbsp;<code>signalSpeed</code>&nbsp;整除的。</li>
19+
<li>从&nbsp;<code>c</code>&nbsp;到&nbsp;<code>b</code>&nbsp;的路径与从&nbsp;<code>c</code>&nbsp;到&nbsp;<code>a</code>&nbsp;的路径没有任何公共边。</li>
20+
</ul>
21+
22+
<p>请你返回一个长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>count</code>&nbsp;,其中&nbsp;<code>count[i]</code> 表示通过服务器&nbsp;<code>i</code>&nbsp;<strong>可连接</strong>&nbsp;的服务器对的&nbsp;<strong>数目</strong>&nbsp;。</p>
23+
24+
<p>&nbsp;</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>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>2 &lt;= n &lt;= 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 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; 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 &lt;= weight<sub>i</sub> &lt;= 10<sup>6</sup></code></li>
60+
<li><code>1 &lt;= signalSpeed &lt;= 10<sup>6</sup></code></li>
61+
<li>输入保证&nbsp;<code>edges</code>&nbsp;构成一棵合法的树。</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)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ tags: [其他, 知识, 英语, Notes]
213213
|oscillation|n. 振荡,振动,摆动<details><summary>例句</summary>Her <font color="#28bea0">oscillations</font> in mood are maddening.<br/>她喜怒无常能把人气疯。</details>|
214214
|hanger|n. 衣架,挂钩,挂东西的人|
215215
|||
216-
|orchard|n. 果园|
216+
|<font color="#28bea0" title="二次复习">orchard</font>|n. 果园|
217217
|||
218218
|mineral|n. 矿物,矿物质|
219219
|turbine|n. 涡轮机,汽轮机|
@@ -226,6 +226,15 @@ tags: [其他, 知识, 英语, Notes]
226226
|rebate|n. 折扣,退还款<br/>v. 退还(部分付款),扣除|
227227
|reservoir|n. 水库,蓄水池,储藏所,仓库,储藏,积蓄,宝库<br/>v. 储藏,蓄积,在...开凿蓄水池|
228228
|jettison|v. 放弃,拜托,除掉<br/>n. 抛弃,(紧急情况下)抛弃货物|
229+
|||
230+
|replenish|v. 补充 ,重新装满|
231+
|spectrum|n. 光谱,声谱,频谱,范围,幅度,系列,层次|
232+
|||
233+
|livestock|n. 家畜, 牲畜|
234+
|||
235+
|stencil|n. (印刷用)模板,(模板印的)文字<br/>v. 用模板印(文字/图案)|
236+
|pluck|v. 摘,拔(去),拨(弦),解救v. 胆识,胆量,内脏,快而猛的拉,勇气|
237+
|daybreak|n. 黎明,破晓|
229238

230239
<details>
231240
<summary>扇贝音频测试</summary>

0 commit comments

Comments
 (0)