All problems
0664EasyGraph Theory

Hub Depot of the Courier Network

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1791Find Center of Star Graph

This 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 courier company runs n depots labelled 1 through n. One of them is the hub: every other depot is joined to the hub by its own road, and there are no other roads, so the network holds exactly n - 1 roads and each road has the hub at one end.

roads lists those roads in no particular order. roads[i] = [a, b] records a road running between depot a and depot b, and the two ends of a road are always different depots.

Return the label of the hub depot.

Examples

Example 1

Input
roads = [[4, 2], [3, 4], [4, 5], [1, 4]]
Output
4

There are four roads, so the network has five depots. Depot `4` is written on every road, and no other depot is.

Example 2

Input
roads = [[9, 7], [7, 2], [7, 10], [1, 7], [7, 5], [3, 7], [7, 8], [6, 7], [7, 4]]
Output
7

Nine roads means ten depots. Depot `7` shares a road with each of the other nine depots, and every road listed has `7` on one side.

Example 3

Input
roads = [[1, 3], [2, 3]]
Output
3

The smallest network of this shape has three depots and two roads. Depot `3` appears on both roads.

Constraints

  • 3 <= n <= 10^5
  • roads.length == n - 1
  • roads[i].length == 2
  • 1 <= a, b <= n
  • a != b
  • The roads describe a network of the shape above, so a hub always exists.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def hub_depot(roads: list[list[int]]) -> int:
Java
public int hubDepot(int[][] roads)
September 7
Apply