You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/3000-3099/3067.Count Pairs of Connectable Servers in a Weighted Tree Network/README_EN.md
+166-4
Original file line number
Diff line number
Diff line change
@@ -55,24 +55,186 @@ It can be shown that no two servers are connectable through servers other than 0
55
55
56
56
## Solutions
57
57
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.
0 commit comments