but it is already given that it is an tree and more it is given that edges are N-1 so it cannot be disconnected and can you give an example for which my DFS is going false.
it is not. but it is fault of codechef problem maker. why these guys not make proper statement like codeforces. in this statement there should be Graph instead of Tree. and that’s why i like codeforces.
In the question it is no where written that u is the parent of v while taking input. It’s written that there is an edge between u and v in the tree. So it’s your mistake for assuming such condition.
it is true. problem at codeforces is really simple to understand but tricky to implement and codechef
has vice-versa game. same as previous year ACM-ICPC qualification round. they do not know difficulty distribution. you can also look for current Long chellange(top 6 problem are too easy and than difficulty of 7 and 8 problem is too high as compared to previous 6 problem).
You don’t know why or you don’t want to know? @raisinten gave a clear idea of where the bug lies, you’re just misinterpreting it. It is mentioned in the problem statement that u and v has an edge between them, you assumed that that edge is directed: that u has to be the parent of v. It is simply connectedness that is given, which means that any of the two can be the parent of the other. Don’t you think if your assumption were true, the level of each node is kind of embedded in the given graph (or well, the tree)? The entire structure of the tree would have been provided to you then, and very little work remains to be done if any. So, per that claim, your adjacency list is the real culprit here because you have cut down all the possible trees that can arise from the given input and reduced it down to what follows your assumption. Hope, it helps