
Now the game can end in three ways :
1) If ever Tom occupies the same node as Jerry, Tom wins.
2) If ever Jerry reaches the Hole, Jerry wins.
3) If ever a position is repeated, the game is a draw.
.png)
In the above graph, Tom has only one choice to go to node 3. Jerry also has only one option to go to node 0. And the first turn will be taken by Jerry so it will go to node 0 and hence win the game. So we will return 1.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’ denoting the number of vertices and the number of edges in the undirected graph.
The next ‘M’ lines each contain two space-separated integers ‘u’ and ‘v’, denoting an edge from vertex ‘u’ to vertex ‘v’.
For each test case, return 0 if the game draws, 1 if Jerry wins the game, or 2 if Tom wins the game.
Output for each test case should be 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
1 <= N <= 50
1 <= M <= 300
1 <= u, v <= N
Time Limit: 1 sec
The basic idea is to do a memory cache search. Here we have 3 dimensions: At time T, the position of Jerry X, and position of Tom Y, so at any time and any position F(T, X, Y) should have a value, either 0 or 1 or 2.
Here 0 means draw, 1 means Jerry wins, and 2 means Tom wins.
Actually here we are doing colouring of the graph so if it’s Jerry’s turn and any of the child of the current node is 1 we will set current node’s value as 1 which means Jerry can come to this current node without being caught by Tom, but if the value of all children is 2 then the value of current node will also be 2 which implies Tom will catch Jerry at this node in any case. We can also understand this intuition by taking the current turn as Tom’s turn in the same way.