Number of Cities at each Distance
A network consisting of M cities and M-1 roads connecting them is given. Cities are labeled with distinct integers within the range[0..(M-1)]
Roads connect cities in such a way that each pair of distinct cities is connected either by a direct road or along a path consisting of direct roads. There is exactly one way to reach any city from any other city. In other words, cities and direct roads from a tree. The number of direct roads that must be traversed is called the distance between these two cities.
Write a function that, given a non-empty zero-indexed array T consisting of M integers describing a network of M cities and M-1 roads, returns an array consisting of M-1 integers, specifying the number of cities positioned at each distance 1, 2, ..., M-1.
Array T describes a network of cities as follows:
If T[P] = Q and P = Q, then P is the capital;
If T[P] = Q and P != Q, then there is a direct road between cities P and Q
For example, given the following array T consisting of ten elements:
Assume that:
M is an integer within the range [1..100,000]
Each element of array T is an integer within the range [0..M-1]
There is exactly one (possibly indirect) connection between any two distinct cities
Complexity:
Expected worst-case time complexity is O(M)
Expected worst-case space complexity is O(M), beyond input storage(not counting the storage required for input arguments)
Complexity: O(M+E) time and O(M) space (since adjacency list is created)
Last updated