Count Pairs of Connectable Servers in a Weighted Tree Network - Problem

You are given an unrooted weighted tree with n vertices representing servers numbered from 0 to n - 1, an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional edge between vertices ai and bi of weight weighti.

You are also given an integer signalSpeed.

Two servers a and b are connectable through a server c if:

  • a < b
  • a != c and b != c
  • The distance from c to a is divisible by signalSpeed
  • The distance from c to b is divisible by signalSpeed
  • The path from c to a and the path from c to b do not share any edges

Return an integer array count of length n where count[i] is the number of server pairs that are connectable through the server i.

Input & Output

Example 1 — Basic Tree Network
$ Input: edges = [[0,1,1],[1,2,5],[2,3,13],[1,4,9],[4,5,2]], signalSpeed = 1
Output: [0,4,0,0,0,0]
💡 Note: With signalSpeed=1, all distances are valid. Server 1 can connect 4 pairs through its central position in the tree structure.
Example 2 — Specific Signal Speed
$ Input: edges = [[0,6,3],[6,4,2],[6,3,3],[4,5,2],[4,1,4],[4,2,1]], signalSpeed = 3
Output: [0,0,0,0,0,0,2]
💡 Note: Only server 6 can act as a hub. Distances must be divisible by 3, creating 2 valid pairs through server 6.
Example 3 — Linear Chain
$ Input: edges = [[0,1,2],[1,2,4]], signalSpeed = 2
Output: [0,1,0]
💡 Note: In a 3-node linear chain, only the middle server (1) can connect the endpoints since both distances are divisible by 2.

Constraints

  • 1 ≤ n ≤ 1000
  • edges.length == n - 1
  • 1 ≤ ai, bi ≤ n - 1
  • ai != bi
  • 1 ≤ weighti ≤ 106
  • 1 ≤ signalSpeed ≤ 106
  • The input represents a valid tree

Visualization

Tap to expand
Count Pairs of Connectable Servers INPUT 0 1 2 3 4 5 1 5 13 9 2 edges = [[0,1,1],[1,2,5], [2,3,13],[1,4,9],[4,5,2]] signalSpeed = 1 n = 6 servers 5 edges (tree) ALGORITHM STEPS 1 For each server c Iterate through all nodes as potential centers 2 DFS from each neighbor Count nodes reachable with dist % signal == 0 3 Subtree counting Track valid nodes per subtree (no shared edges) 4 Count pairs Multiply counts from different subtrees Example: Server 1 1 0 2 3 4 5 3 subtrees Pairs: 4 FINAL RESULT Connectable Pairs per Server i=0 0 i=1 4 i=2 0 i=3 0 i=4 0 i=5 0 Output: [0, 4, 0, 0, 0, 0] Why Server 1 has 4 pairs? signalSpeed=1 divides all distances, so all nodes valid. Subtrees from node 1: - via 0: 1 node - via 2: 2 nodes (2,3) - via 4: 2 nodes (4,5) 1*2 + 1*2 + 2*2 = 4 Key Insight: For each server c, use DFS to explore each neighbor's subtree separately. Count nodes where distance from c is divisible by signalSpeed. Pairs from different subtrees have non-overlapping paths. Total pairs = sum of products of counts from all pairs of subtrees. TutorialsPoint - Count Pairs of Connectable Servers in a Weighted Tree Network | DFS with Subtree Counting
Asked in
Meta 12 Google 8 Amazon 6
8.9K Views
Medium Frequency
~35 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen