The country of Dadorlandtiria has \(n\) cities, numbered from \(0\) to \(n-1\). The country also has exactly \(n\) direct roads connecting all the cities. Namely, for every \(0 \le i \le n-1\), cities \(u[i]\) and \(v[i]\) are connected by a direct road.
Notice that all roads in Dadorlandtiria are in need of repair. The country's leader wants to know how inconvenient repairing each road would be for the population.
Specifically, each day some road is closed for repair works. The inconvenience level is defined as the pair of cities between which the travel time is the longest. Note that it may also become impossible to travel between some pairs of cities.
For each road, find the longest travel time in the country after that road is closed. If travel between some cities becomes impossible, return \(-1\) as the answer.
You should implement the following procedure:
int[] find_diameters(int n, int[] u, int[] v)
Consider the following call:
find_diameters(4, [1, 1, 0, 3], [0, 2, 3, 1])
Initially, Dadorlandtiria looks like this (cities \(0,1,2,3\) with roads \(1\!-\!0\), \(1\!-\!2\), \(0\!-\!3\), \(3\!-\!1\)):
Therefore, find_diameters() should return the array \([3, -1, 2, 3]\).
The sample grader reads the input in the following format:
Suppose find_diameters() returns the array \(r\). The sample grader prints: