Getting W/A in this question, please help with logic correction?

hello @shibangi

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