
The list should be in the order of increasing frequency of restaurant name. If the frequency of restaurants is the same then order them in alphabetical order.
The first line contains an integer ‘T’, which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains an integer ‘N’ denoting the number of safe points.
The next alternate ‘N’ lines contain integer ‘X’ denoting the number of strings in a single array followed by space-separated strings denoting the restaurant names.
The next alternate ‘N’ lines contain integer ‘Y’ denoting the number of integers in a single array followed by space-separated integers denoting the link between the safe points.
Last line contains two space-separated integers “Id” and “level” (separated by space).
For each test case, print a single containing space-separated strings denoting the restaurant names.
The output of each test case will be printed in a separate line.
You don’t need to print anything, It has already been taken care of. Just implement the given function.
1 <= T <= 10
2 <= N <= 20
1 <= restaurants[i].length OR (‘X’) <= 10
1 <= restaurants[i][j].length <= 8
0 <= safePoints[i].length OR (‘Y’) < N
0 <= safePoints[i][j] < N
0 <= id < N
1 <= level < N
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of safe points, ‘i’ and ‘j’ denote the ith and jth index respectively.
Time limit: 1 sec.
The approach is to use Breadth-First Search of the safePoints array (which denotes all the connections between safe points). First, we will insert the given “Id” in the queue “bfsQueue”(which will be used for bfs) and will continue to add its neighbors in the queue through iteration. At every new step, we will increment our counter “currentLevel” and break the iteration when this counter “currentLevel” will be equal to the given Level.
After this, we will be at the required level. Now we will generate a map “bfsMap” in which we will store the restaurant’s name (of the required safePoints) along with the frequency.
Now we will simply sort the names of the restaurants with their frequencies.