Ninja just gave his board exams. Now he wants to go on a vacation consisting of ‘N’ cities connected by ‘M’ bidirectional roads. He wants to know in how many ways he can visit all the cities such that the order of visiting cities is different each time. He can visit an already visited city again to reach other cities. Since the number of ways can be too large, print the number modulo 1000000007.
Example :
A way to reach city 1 from city 3 can be: 32421
Here we revisited city 2 to reach city 1.
A way to reach city 3 from city 1 can be: 12423
Here we revisited city 2 to reach city 3.
Orders 32421 and 12423 are to be considered different.
The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains two single space-separated integers, ‘N’ and ‘M’, representing the number of cities and the number of roads, respectively.
The next ‘M’ lines will denote the road between city ‘X’ and city ‘Y’ defined by two single space-separated integers ‘X' and 'Y'.
Output Format :
For each test case, print the number of ways in which cities can be visited modulo 1000000007.
Print the output of each test case in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10
N - 1 <= M <= N*(N - 1) / 2
1 <= X, Y <= N and X != Y
Time Limit : 1 sec
1
4 6
1 2
3 2
1 3
1 4
3 4
2 4
24
The routes will be :
Some of the paths can be: {1234, 1432, 2341,...}
There is a road between each pair of cities. So the number of possible ways can be 4! = 24.
1
4 4
1 2
2 3
1 3
2 4
14
The routes will be :
City 4 can be reached via road between city 2 and city 4. The following patterns are not possible :
4 1 * * (2 patterns)
4 3 * * (2 patterns)
1 4 * * (2 patterns)
3 4 * * (2 patterns)
1 3 4 2
3 1 4 2
So possible ways are : Total ways - Not possible ways = 24 - 10 = 14
Try to think of generating all possible paths and validating them.
The basic idea is to generate all permutations in which cities can be visited. For each permutation, we create a map to store the adjacent cities. If a city is not present in the map then the path will not be valid, else valid.
Example:
Let a permutation be: 1 3 2 4
We create a map and insert the first element of permutation initially into it. Map : {1}
We now add all adjacent cities of 1 into the map, so the map becomes: {1, 2, 3, 4}
We move to the next element in the permutation, i.e., 3 which is present in the map and add its adjacent cities into the map.
As all the cities are present in the map, so the permutation will be valid.
Now, let a permutation be: 1 4 3 2
Similarly, we will create a map and insert 1 into the map, so the map biomes: {1}
We will insert cities adjacent to 1 into the map, so the map now becomes: {1, 2, 3}
We move to the next element in the permutation, i.e., 4 which is not present in the map.
The permutation becomes invalid as 4 was not present in the map.
We can do this for all the permutations to count the number of valid ways.
Here is the algorithm :
HELPER(‘GRAPH’, ‘ARR’, ‘SI’, ‘EI, ‘RES’)
CALC(‘GRAPH’, ‘ARR’, ‘N’)
O(N!*(N^2)), where ‘N’ is the number of cities.
We generate all permutations in which cities can be visited which take N! of time and for each permutation, we traverse it and check whether it’s a valid permutation or not. We traverse all the elements of a permutation and for each element, we traverse its adjacent cities which can up to ‘N’ - 1. Therefore, the overall time complexity will be O(N!*(N^2)).
O(N + M), where ‘N’ is the number of cities and ‘M’ is the number of roads.
For each city, we store its adjacent cities, therefore storing the cities in the form of a graph will require O(N + M) space. We also use an array of size ‘N’ to store the permutation. We use a map also to store the adjacent cities. The recursive stack to generate permutation can have maximum ‘N’ calls. Therefore, the overall space complexity will be O(N + M).