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

Imagine you're designing a network communication system for a company with n servers arranged in a tree structure. Each connection between servers has a specific weight representing the communication cost.

Your task is to determine how many pairs of servers can communicate through each intermediate server, given a specific signal speed constraint.

Communication Rules:

  • Two servers a and b can communicate through server c if:
  • a < b (to avoid counting pairs twice)
  • Both a and b are different from c
  • The distance from c to a is divisible by signalSpeed
  • The distance from c to b is divisible by signalSpeed
  • The paths from c to a and from c to b don't share any edges

Goal: Return an array where count[i] represents the number of server pairs that can communicate through server i.

Input & Output

example_1.py โ€” 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: Server 1 can relay 4 pairs: (0,2), (0,3), (0,4), (0,5) since all distances are divisible by 1. Other servers have no valid pairs due to tree structure constraints.
example_2.py โ€” Signal Speed Constraint
$ Input: edges = [[0,6,3],[6,4,2],[6,3,3],[4,5,2],[5,1,3],[3,2,3]], signalSpeed = 3
โ€บ Output: [2,0,0,1,0,0,0]
๐Ÿ’ก Note: With signalSpeed=3, only distances divisible by 3 are valid. Server 0 can relay 2 pairs, server 3 can relay 1 pair.
example_3.py โ€” Linear Tree
$ Input: edges = [[0,1,2],[1,2,4]], signalSpeed = 2
โ€บ Output: [0,1,0]
๐Ÿ’ก Note: In a linear tree with 3 nodes, only the middle server (1) can act as relay. Distance 0โ†’1=2, distance 1โ†’2=4, both divisible by 2, so 1 pair (0,2).

Visualization

Tap to expand
RelayABCDdist=6dist=9dist=3dist=12Server Communication NetworkSignal Speed = 3 (distances must be divisible by 3)Valid Pairs:โœ“ (A,C): 6,3 โ†’ both รท 3โœ— (A,B): 6,9 โ†’ not same subtreeโœ— (A,D): 6,12 โ†’ D not รท 3
Understanding the Visualization
1
Tree Structure
Servers are connected in a tree - no cycles, exactly n-1 edges
2
Signal Propagation
Signals travel along edges with cumulative weights as distances
3
Relay Requirements
Distance from relay to both endpoints must be divisible by signalSpeed
4
Path Independence
Paths to different subtrees from relay never share edges in a tree
Key Takeaway
๐ŸŽฏ Key Insight: In trees, we can count valid pairs efficiently by grouping nodes into subtrees and leveraging the fact that inter-subtree paths never share edges.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

For each of n servers, we run DFS (O(n)) then check all pairs (O(nยฒ))

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for adjacency list and recursion stack during DFS

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 1000
  • edges.length == n - 1
  • edges[i].length == 3
  • 0 โ‰ค ai, bi < n
  • ai โ‰  bi
  • 1 โ‰ค weighti, signalSpeed โ‰ค 106
  • The edges form a valid tree
Asked in
Meta 35 Google 28 Amazon 22 Microsoft 18
23.4K Views
Medium Frequency
~25 min Avg. Time
847 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