Skip to content

Commit 7cb44df

Browse files
authored
feat: add solutions to lc problem: No.3067 (doocs#2412)
No.3067.Count Pairs of Connectable Servers in a Weighted Tree Network
1 parent 2e385b5 commit 7cb44df

File tree

7 files changed

+487
-8
lines changed

7 files changed

+487
-8
lines changed

solution/3000-3099/3067.Count Pairs of Connectable Servers in a Weighted Tree Network/README.md

+166-4
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,186 @@
6363

6464
## 解法
6565

66-
### 方法一
66+
### 方法一:枚举 + DFS
67+
68+
我们先根据题目给定的边构建出一个邻接表 $g$,其中 $g[a]$ 表示节点 $a$ 的所有邻居节点以及对应的边权。
69+
70+
然后,我们可以枚举每一个节点 $a$ 作为连接的中间节点,通过深度优先搜索计算出从 $a$ 的邻居节点 $b$ 出发的,且到节点 $a$ 的距离可以被 $signalSpeed$ 整除的节点数 $t$。那么,节点 $a$ 的可连接节点对数目增加了 $s \times t$,其中 $s$ 表示节点 $a$ 的邻居节点 $b$ 出发的,且到节点 $a$ 的距离不可以被 $signalSpeed$ 整除的累计节点数。然后我们更新 $s$ 为 $s + t$。
71+
72+
枚举完所有节点 $a$ 之后,我们就可以得到所有节点的可连接节点对数目。
73+
74+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 表示节点数。
6775

6876
<!-- tabs:start -->
6977

7078
```python
71-
79+
class Solution:
80+
def countPairsOfConnectableServers(
81+
self, edges: List[List[int]], signalSpeed: int
82+
) -> List[int]:
83+
def dfs(a: int, fa: int, ws: int) -> int:
84+
cnt = 0 if ws % signalSpeed else 1
85+
for b, w in g[a]:
86+
if b != fa:
87+
cnt += dfs(b, a, ws + w)
88+
return cnt
89+
90+
n = len(edges) + 1
91+
g = [[] for _ in range(n)]
92+
for a, b, w in edges:
93+
g[a].append((b, w))
94+
g[b].append((a, w))
95+
ans = [0] * n
96+
for a in range(n):
97+
s = 0
98+
for b, w in g[a]:
99+
t = dfs(b, a, w)
100+
ans[a] += s * t
101+
s += t
102+
return ans
72103
```
73104

74105
```java
75-
106+
class Solution {
107+
private int signalSpeed;
108+
private List<int[]>[] g;
109+
110+
public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) {
111+
int n = edges.length + 1;
112+
g = new List[n];
113+
this.signalSpeed = signalSpeed;
114+
Arrays.setAll(g, k -> new ArrayList<>());
115+
for (var e : edges) {
116+
int a = e[0], b = e[1], w = e[2];
117+
g[a].add(new int[] {b, w});
118+
g[b].add(new int[] {a, w});
119+
}
120+
int[] ans = new int[n];
121+
for (int a = 0; a < n; ++a) {
122+
int s = 0;
123+
for (var e : g[a]) {
124+
int b = e[0], w = e[1];
125+
int t = dfs(b, a, w);
126+
ans[a] += s * t;
127+
s += t;
128+
}
129+
}
130+
return ans;
131+
}
132+
133+
private int dfs(int a, int fa, int ws) {
134+
int cnt = ws % signalSpeed == 0 ? 1 : 0;
135+
for (var e : g[a]) {
136+
int b = e[0], w = e[1];
137+
if (b != fa) {
138+
cnt += dfs(b, a, ws + w);
139+
}
140+
}
141+
return cnt;
142+
}
143+
}
76144
```
77145

78146
```cpp
79-
147+
class Solution {
148+
public:
149+
vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {
150+
int n = edges.size() + 1;
151+
vector<pair<int, int>> g[n];
152+
for (auto& e : edges) {
153+
int a = e[0], b = e[1], w = e[2];
154+
g[a].emplace_back(b, w);
155+
g[b].emplace_back(a, w);
156+
}
157+
function<int(int, int, int)> dfs = [&](int a, int fa, int ws) {
158+
int cnt = ws % signalSpeed == 0;
159+
for (auto& [b, w] : g[a]) {
160+
if (b != fa) {
161+
cnt += dfs(b, a, ws + w);
162+
}
163+
}
164+
return cnt;
165+
};
166+
vector<int> ans(n);
167+
for (int a = 0; a < n; ++a) {
168+
int s = 0;
169+
for (auto& [b, w] : g[a]) {
170+
int t = dfs(b, a, w);
171+
ans[a] += s * t;
172+
s += t;
173+
}
174+
}
175+
return ans;
176+
}
177+
};
80178
```
81179
82180
```go
181+
func countPairsOfConnectableServers(edges [][]int, signalSpeed int) []int {
182+
n := len(edges) + 1
183+
type pair struct{ x, w int }
184+
g := make([][]pair, n)
185+
for _, e := range edges {
186+
a, b, w := e[0], e[1], e[2]
187+
g[a] = append(g[a], pair{b, w})
188+
g[b] = append(g[b], pair{a, w})
189+
}
190+
var dfs func(a, fa, ws int) int
191+
dfs = func(a, fa, ws int) int {
192+
cnt := 0
193+
if ws%signalSpeed == 0 {
194+
cnt++
195+
}
196+
for _, e := range g[a] {
197+
b, w := e.x, e.w
198+
if b != fa {
199+
cnt += dfs(b, a, ws+w)
200+
}
201+
}
202+
return cnt
203+
}
204+
ans := make([]int, n)
205+
for a := 0; a < n; a++ {
206+
s := 0
207+
for _, e := range g[a] {
208+
b, w := e.x, e.w
209+
t := dfs(b, a, w)
210+
ans[a] += s * t
211+
s += t
212+
}
213+
}
214+
return ans
215+
}
216+
```
83217

218+
```ts
219+
function countPairsOfConnectableServers(edges: number[][], signalSpeed: number): number[] {
220+
const n = edges.length + 1;
221+
const g: [number, number][][] = Array.from({ length: n }, () => []);
222+
for (const [a, b, w] of edges) {
223+
g[a].push([b, w]);
224+
g[b].push([a, w]);
225+
}
226+
const dfs = (a: number, fa: number, ws: number): number => {
227+
let cnt = ws % signalSpeed === 0 ? 1 : 0;
228+
for (const [b, w] of g[a]) {
229+
if (b != fa) {
230+
cnt += dfs(b, a, ws + w);
231+
}
232+
}
233+
return cnt;
234+
};
235+
const ans: number[] = Array(n).fill(0);
236+
for (let a = 0; a < n; ++a) {
237+
let s = 0;
238+
for (const [b, w] of g[a]) {
239+
const t = dfs(b, a, w);
240+
ans[a] += s * t;
241+
s += t;
242+
}
243+
}
244+
return ans;
245+
}
84246
```
85247

86248
<!-- tabs:end -->

solution/3000-3099/3067.Count Pairs of Connectable Servers in a Weighted Tree Network/README_EN.md

+166-4
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,186 @@ It can be shown that no two servers are connectable through servers other than 0
5555

5656
## Solutions
5757

58-
### Solution 1
58+
### Solution 1: Enumeration + DFS
59+
60+
First, we construct an adjacency list `g` based on the edges given in the problem, where `g[a]` represents all the neighbor nodes of node `a` and their corresponding edge weights.
61+
62+
Then, we can enumerate each node `a` as the connecting intermediate node, and calculate the number of nodes `t` that start from the neighbor node `b` of `a` and whose distance to node `a` can be divided by `signalSpeed` through depth-first search. Then, the number of connectable node pairs of node `a` increases by `s * t`, where `s` represents the cumulative number of nodes that start from the neighbor node `b` of `a` and whose distance to node `a` cannot be divided by `signalSpeed`. Then we update `s` to `s + t`.
63+
64+
After enumerating all nodes `a`, we can get the number of connectable node pairs for all nodes.
65+
66+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the number of nodes.
5967

6068
<!-- tabs:start -->
6169

6270
```python
63-
71+
class Solution:
72+
def countPairsOfConnectableServers(
73+
self, edges: List[List[int]], signalSpeed: int
74+
) -> List[int]:
75+
def dfs(a: int, fa: int, ws: int) -> int:
76+
cnt = 0 if ws % signalSpeed else 1
77+
for b, w in g[a]:
78+
if b != fa:
79+
cnt += dfs(b, a, ws + w)
80+
return cnt
81+
82+
n = len(edges) + 1
83+
g = [[] for _ in range(n)]
84+
for a, b, w in edges:
85+
g[a].append((b, w))
86+
g[b].append((a, w))
87+
ans = [0] * n
88+
for a in range(n):
89+
s = 0
90+
for b, w in g[a]:
91+
t = dfs(b, a, w)
92+
ans[a] += s * t
93+
s += t
94+
return ans
6495
```
6596

6697
```java
67-
98+
class Solution {
99+
private int signalSpeed;
100+
private List<int[]>[] g;
101+
102+
public int[] countPairsOfConnectableServers(int[][] edges, int signalSpeed) {
103+
int n = edges.length + 1;
104+
g = new List[n];
105+
this.signalSpeed = signalSpeed;
106+
Arrays.setAll(g, k -> new ArrayList<>());
107+
for (var e : edges) {
108+
int a = e[0], b = e[1], w = e[2];
109+
g[a].add(new int[] {b, w});
110+
g[b].add(new int[] {a, w});
111+
}
112+
int[] ans = new int[n];
113+
for (int a = 0; a < n; ++a) {
114+
int s = 0;
115+
for (var e : g[a]) {
116+
int b = e[0], w = e[1];
117+
int t = dfs(b, a, w);
118+
ans[a] += s * t;
119+
s += t;
120+
}
121+
}
122+
return ans;
123+
}
124+
125+
private int dfs(int a, int fa, int ws) {
126+
int cnt = ws % signalSpeed == 0 ? 1 : 0;
127+
for (var e : g[a]) {
128+
int b = e[0], w = e[1];
129+
if (b != fa) {
130+
cnt += dfs(b, a, ws + w);
131+
}
132+
}
133+
return cnt;
134+
}
135+
}
68136
```
69137

70138
```cpp
71-
139+
class Solution {
140+
public:
141+
vector<int> countPairsOfConnectableServers(vector<vector<int>>& edges, int signalSpeed) {
142+
int n = edges.size() + 1;
143+
vector<pair<int, int>> g[n];
144+
for (auto& e : edges) {
145+
int a = e[0], b = e[1], w = e[2];
146+
g[a].emplace_back(b, w);
147+
g[b].emplace_back(a, w);
148+
}
149+
function<int(int, int, int)> dfs = [&](int a, int fa, int ws) {
150+
int cnt = ws % signalSpeed == 0;
151+
for (auto& [b, w] : g[a]) {
152+
if (b != fa) {
153+
cnt += dfs(b, a, ws + w);
154+
}
155+
}
156+
return cnt;
157+
};
158+
vector<int> ans(n);
159+
for (int a = 0; a < n; ++a) {
160+
int s = 0;
161+
for (auto& [b, w] : g[a]) {
162+
int t = dfs(b, a, w);
163+
ans[a] += s * t;
164+
s += t;
165+
}
166+
}
167+
return ans;
168+
}
169+
};
72170
```
73171
74172
```go
173+
func countPairsOfConnectableServers(edges [][]int, signalSpeed int) []int {
174+
n := len(edges) + 1
175+
type pair struct{ x, w int }
176+
g := make([][]pair, n)
177+
for _, e := range edges {
178+
a, b, w := e[0], e[1], e[2]
179+
g[a] = append(g[a], pair{b, w})
180+
g[b] = append(g[b], pair{a, w})
181+
}
182+
var dfs func(a, fa, ws int) int
183+
dfs = func(a, fa, ws int) int {
184+
cnt := 0
185+
if ws%signalSpeed == 0 {
186+
cnt++
187+
}
188+
for _, e := range g[a] {
189+
b, w := e.x, e.w
190+
if b != fa {
191+
cnt += dfs(b, a, ws+w)
192+
}
193+
}
194+
return cnt
195+
}
196+
ans := make([]int, n)
197+
for a := 0; a < n; a++ {
198+
s := 0
199+
for _, e := range g[a] {
200+
b, w := e.x, e.w
201+
t := dfs(b, a, w)
202+
ans[a] += s * t
203+
s += t
204+
}
205+
}
206+
return ans
207+
}
208+
```
75209

210+
```ts
211+
function countPairsOfConnectableServers(edges: number[][], signalSpeed: number): number[] {
212+
const n = edges.length + 1;
213+
const g: [number, number][][] = Array.from({ length: n }, () => []);
214+
for (const [a, b, w] of edges) {
215+
g[a].push([b, w]);
216+
g[b].push([a, w]);
217+
}
218+
const dfs = (a: number, fa: number, ws: number): number => {
219+
let cnt = ws % signalSpeed === 0 ? 1 : 0;
220+
for (const [b, w] of g[a]) {
221+
if (b != fa) {
222+
cnt += dfs(b, a, ws + w);
223+
}
224+
}
225+
return cnt;
226+
};
227+
const ans: number[] = Array(n).fill(0);
228+
for (let a = 0; a < n; ++a) {
229+
let s = 0;
230+
for (const [b, w] of g[a]) {
231+
const t = dfs(b, a, w);
232+
ans[a] += s * t;
233+
s += t;
234+
}
235+
}
236+
return ans;
237+
}
76238
```
77239

78240
<!-- tabs:end -->

0 commit comments

Comments
 (0)