Sir, i'm getting correct output but fail in the test cases. Please help me to figure out my mistake

hello @chandreshmaurya

ur logic is correct.

the issue is with the constrcution of graph.
u r assuming that if x–y edge is given then x will be the parent of y.
which is wrong.
the parent and child will be decided by the least value of that component.

for example->
3–4–1 (they all are in a single component)

here 1 is least value so 1 is master parent.
and its child will be 4. and 4 child will be 3
so parent- child graph should be like this->
1 --> 4–>3

sir, in the given input -> (1 2, 2 3 , 2 4) . are 1 is parent of 2 and 2 is parent of 3 & 4 ?

yes…

…

check this for clarity->

sir, i got your code. But, i want to know 2 things. (1) ->for which test case, my code is failing and (2) -> is there any modification is passible in my code?

…

u r coding based on assumption that if a—b edge is given then a is parent of b which is wrong. u need add same parent and child array and made undirected graph to make it work

ohkay sir done … Thank you

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.