Trains the technique from
LeetCode 3067Count Pairs of Connectable Servers in a Weighted Tree NetworkThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A campus network wires n relay nodes, numbered 0 to n - 1, into a tree. The wiring arrives as a weighted edge list links, where links[i] = [a, b, w] means a cable of length w runs between node a and node b. Since the network is a tree, links holds n - 1 cables and exactly one route runs between any two nodes. The distance between two nodes is the total cable length along that route.
The gear runs at a fixed cadence hop. Fix a node c and call it the hub. An unordered pair of nodes {a, b} relays through c when all of the following hold:
a, b and c are three different nodes;a to b passes through c;a to c is a multiple of hop;c to b is a multiple of hop.Produce a list of length n whose entry at position c is how many unordered pairs relay through node c.
Example 1
From node 0 the branch through node 1 holds nodes 1 and 3 at distances 2 and 4, and the branch through node 2 holds nodes 2 and 4 at distances 2 and 6; all four distances are multiples of 2, so the four pairs taking one node from each branch all relay through node 0. Node 1 has nodes 0, 2 and 4 at distances 2, 4 and 8 on one side and node 3 at distance 2 on the other, giving three pairs, and node 2 works out the same way. Nodes 3 and 4 each have a single cable, so no route can enter and leave them.
Example 2
At node 1 the pair {0, 2} qualifies: node 0 is 3 away, node 2 is 3 away and the route between them runs through node 1. At node 0 the only other branch holds node 3 at distance 1, which is not a multiple of 3, so nothing pairs there. Nodes 2 and 3 are ends of the network.
Example 3
The network has two nodes, so no pair can supply two endpoints distinct from the hub and both counts are zero.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def relay_pairs(links: list[list[int]], hop: int) -> list[int]:public int[] relayPairs(int[][] links, int hop)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.