you can test this with any language at https://repl.it/
int [] getChildren(int s, int[][] roads, int p){
List<Integer> children = new ArrayList<>();
for(int i = 0; i < roads.length; i++){
if(roads[i][0] == s && roads[i][1] != p){
children.add(roads[i][1]);
}
if(roads[i][1] == s && roads[i][0] != p){
children.add(roads[i][0]);
}
}
return children
.stream()
.distinct()
.mapToInt(Integer::intValue)
.toArray();
}
boolean efficientRoadNetwork(int n, int[][] roads) {
for(int i = 0; i < n; i++){
int total = 0;
int[] children = getChildren(i, roads, -1);
System.out.println("children");
// this is a one level tree
if(children.length + 1 == n){
return true;
}
total += children.length;
Arrays.stream(children).forEach(System.out::println);
// check second level only
for(int j = 0; j < children.length; j++){
int[] _innerChildren = getChildren(children[j],roads,i);
List<Integer> innerChildren = new ArrayList<>();
for(int _i : _innerChildren){
if(Arrays.asList(children).contains(_i)){
innerChildren.add(_i);
}
}
System.out.println("inner children");
total += innerChildren.size();
}
if(total < n - 1){
return false;
}
}
return true;
}
Once upon a time, in a kingdom far, far away, there lived a king Byteasar III. As a smart and educated ruler, he did everything in his (unlimited) power to make every single system of his kingdom efficient. The king went down in history as a great road builder: during his reign a great number of roads were built, and the road system he created was the most efficient for those dark times.
When you started to learn about Byteasar's III deeds in your history classes, the creeping doubt crawled into the back of your mind: what if the famous king wasn't so great after all? According to the most recent studies, there were n cities in the Byteasar's kingdom, which were connected by the two-ways roads. You managed to get information about this roads from the university library, so now you can write a function that will determine whether the road system in Byteasar's kingdom was efficient or not.
A road system is considered efficient if it is possible to travel from any city to any other city by traversing at most 2 roads.
Example
For n = 6 and
roads = [[3, 0], [0, 4], [5, 0], [2, 1],
[1, 4], [2, 3], [5, 2]]
the output should be
efficientRoadNetwork(n, roads) = true.
Here's how the road system can be represented:
For n = 6 and
roads = [[0, 4], [5, 0], [2, 1],
[1, 4], [2, 3], [5, 2]]
the output should be
efficientRoadNetwork(n, roads) = false.
Here's how the road system can be represented:
As you can see, it's only possible to travel from city 3 to city 4 by traversing at least 3 roads.
Input/Output
[execution time limit] 3 seconds (java)
[input] integer n
The number of cities in the kingdom.
Guaranteed constraints:
1 ≤ n ≤ 250.
[input] array.array.integer roads
Array of roads in the kingdom. Each road is given as a pair [cityi, cityj], where 0 ≤ cityi, cityj < n and cityi ≠ cityj. It is guaranteed that no road is given twice.
Guaranteed constraints:
0 ≤ roads.length ≤ 35000,
roads[i].length = 2,
0 ≤ roads[i][j] < n.
[output] boolean
true if the road system is efficient, false otherwise.
[Java] Syntax Tips
// Prints help message to the console
// Returns a string
//
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
System.out.println("This prints to the console when you Run Tests");
return "Hello, " + name;
}