Timer is Running

Castle RUN
Submissions: 2585   Accuracy:


  Difficulty: Medium   Marks: 4

Mayank once came to New Delhi for an International Mathematics Conference. His mathematics skills were a great topic of discussion in those days. So a mathematician Harsh “Challenged” Mayank for a competition and Mayank accepted it.

Rules: They will ask 3 questions each and the one who will give all the answers right will be the winner.
Mayank answered all the questions very easily. Now its time for Harsh to answer the questions but Harsh was confused by the very first question now you have to help so let's read the question.

Question: Mayank asked that you are in a beautiful Castle which is huge and contains ’N’ number of rooms and each room contains 'M' path to ‘M’ other rooms. There may be only one path which connects two room i.e either one path or no path. Mayank asked that you have to find whether you can reach back to the room from which you started after travelling all the paths but note that once you travel a path, then it will be closed and make sure all the rooms are travelled atleast once. In general all paths are to be travelled exactly once and each room must be travelled atleast once. Note: If any room is not connected print False and if there is only one room then print True.

The first line will 
contain an integer T(number of test cases). Each test case will contain an Integer ’N’ i.e.number of rooms and next ’N’ lines will contain ’N’ integers each stating whether there is a path between room ni & nj.

Note: If no path exists then the value is -1, if the room is same then the value will be 0 and if a path exists the value will be 1. Path between room ni & nj is both ways.

For each test case, print "True" if you can reach back else print "False".


0 1 1 1 1
1 0 -1 1 -1
1 -1 0 1 -1
1 1 1 0 1
1 -1 -1 1 0

There can be a path from r1->r3->r4->r5->r1->r4->r2->r1 where r1,r2,r3,r4,r5 are rooms. Here all paths are covered and r1 is reached again.


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harsh Goyal
Author: Harsh-Goyal

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.