Let’s take the exemple of the question :

```
1
4
2 1 3
1 2
2 3
2 4
```

My algorithm for this exemple :

Take a node (let’s say 2). Add 2 to the indexed set.

Then, I will go through all of the unvisited neighbours of 2. Add them (node 1,3 and 4) to the indexed set and calculate* the number of answers for them. After this we can calculate* the number of answers for 2.

(*) We can see that we can calculate the number of solutions for a current node (taking it as the middle), knowing the number of nodes bigger than the current node and smaller than the current node.

My solution https://www.codechef.com/viewsolution/25691825

(functions : function isStar is for checking if the graph is a star and function star is to calculate the star case in O(n))