Skip to content

Practice Count Pairs of Connectable Servers in a Weighted Tree Network

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3067
LeetCode Problem

Count Pairs of Connectable Servers in a Weighted Tree Network

Problem Statement

The edges [a, b, weight] form an undirected weighted tree of n servers numbered 0 through n - 1. Given signalSpeed, return one count per server c. Count unordered pairs of distinct other servers a and b for which the distances from c to a and from c to b are both divisible by signalSpeed, and the two paths starting at c share no edge. Count each pair once, equivalently using a < b.

Source: Count Pairs of Connectable Servers in a Weighted Tree Network on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1,1],[1,2,5],[2,3,13],[3,4,9],[4,5,2]], signalSpeed = 1
Output
[0,4,6,6,4,0]

# Example 2

Input
edges = [[0,6,3],[6,5,3],[0,3,1],[3,2,7],[3,1,6],[3,4,2]], signalSpeed = 3
Output
[2,0,0,0,0,0,2]

Constraints

2 <= n <= 1000
edges.length == n - 1
edges[i].length == 3
0 <= ai, bi < n
edges[i] = [ai, bi, weighti]
1 <= weighti <= 10^6
1 <= signalSpeed <= 10^6
The input is generated such that edges represents a valid tree.

Related LeetCode Problems

Practice more problems similar to Count Pairs of Connectable Servers in a Weighted Tree Network.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.