Ninja has been given two Graphs: a directed graph ‘DIRGRAPH’ and an undirected graph ‘UNDIRGRAPH’. Ninja has to count the number of triangles in each of the graphs.
Can you help Ninja with finding the number of triangles in ‘DIRGRAPH’ and ‘UNDIRGRAPH’.
For Example :

In this example, this graph is directed and there are two triangles possible.
Triangle 1 : (0 3 2)
Triangle 2 : (0 1 2)

In this example, this graph is undirected and there are two triangles possible.
Triangle 1 : (0 3 2)
Triangle 2 : (0 1 2)
The first line of input contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains two space-separated integers ‘V1’ and ‘E1’, which represents the number of vertices present in ‘DIR_GRAPH’ and the number of edges present in ‘DIR_GRAPH’.
The next ‘E1’ lines of each test case contain two single space-separated integers, that denote that there exists an edge between vertex a1 and b1.
The next lines of each test case contain two space-separated integers ‘V2’ and ‘E2’, which represent the number of vertices present in ‘UNDIR_GRAPH’ and the number of edges present in ‘UNDIR_GRAPH’.
The next ‘E2’ lines of each test case contain two single space-separated integers, that denote that there exists an edge between vertex a2 and b2.
Output Format :
For each test case, print the number of triangles in ‘DIR_GRAPH’ and ‘UNDIR_GRAPH’.
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’ <= 100
2 <= ‘V1’,’V2’ <= 1000
1 <= ’E1’ <= (V1 * (V1 - 1)) / 2
1 <= ’E2’ <= (V2 * (V2 - 1)) / 2
0 <= ‘a1’, ‘b1’ <= ‘V1’ - 1
0 <= ‘a2’, ‘b2’ <= ‘V2’ - 1
Time Limit: 1 sec
1
4 5
0 1
1 2
2 0
0 3
3 2
4 5
0 1
1 2
2 0
0 3
3 2
2 2
For sample test case 1 :

For a directed graph, there are two triangles possible.
Triangle 1 : (0 3 2)
Triangle 2 : (0 1 2)

For an undirected graph, there are two triangles possible.
Triangle 1 : (0 3 2)
Triangle 2 : (0 1 2)
1
5 7
0 3
3 2
3 4
2 4
2 0
1 0
1 2
5 7
0 3
3 2
3 4
2 4
2 0
1 0
1 2
1 3
For sample test case 1 :

For a directed graph, there is only one triangle possible.
Triangle 1 : (0 3 2)

For an undirected graph, there are three triangles possible.
Triangle 1 : (0 3 2)
Triangle 2 : (0 1 2)
Triangle 3 : (2 3 4)
Think of Brute Force Approach.
First, we run three nested loops to find all the triplets ‘VER1’, ‘VER2’, and ‘VER3’ where VER1’, ‘VER2’, and ‘VER3’ represents our first, second, and third vertices of the given graph.
The ‘UNDIR_GRAPH’ triplets ‘VER1’, ‘VER2’, and ‘VER3’ can be permuted to give six combinations. So we divide the total ‘COUNT_TRIANGLE_In_UNDIR_GRAPH’ by 6 to get the actual numbers of triangles.
For example :
If ‘VER1’ = 1
‘VER2’ = 2
‘VER3’ = 3.
Then all possible permutations are { (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1),(3, 1, 2), (3, 2, 1) }.
The ‘DIR_GRAPH’ triplets ‘VER1’, ‘VER2’, and ‘VER3’ can be permuted to give three combinations because, in a directed graph, the order of vertices is essential. So we divide the total ‘COUNT_TRIANGLE_In_DIR_GRAPH’ by 3 to get the actual numbers of triangles.
For example:
If ‘VER1’ = 1
‘VER2’ = 2
‘VER3’ = 3.
Then all possible permutations are { (1, 2, 3), (2, 3, 1), (3, 1, 2) }
Algorithm:
O(max(V1, V2) ^ 3) Where ‘V1’ and ‘V2’ are the numbers of vertices of ‘DIR_GRAPH’ and ‘UNDIR_GRAPH’.
Because we are using three nested loops. So the overall complexity is O(max(V1, V2) ^ 3).
O(max(V1, V2) ^ 2). Where ‘V1’ and ‘V2’ are the numbers of vertices of ‘DIR_GRAPH’ and ‘UNDIR_GRAPH’.
Because we are creating an adjacency matrix for both graphs. So space complexity will be O(max(V1, V2) ^ 2).