Getting wrong answer in "frogv"

problem : CodeChef: Practical coding for everyone
my solution : CodeChef: Practical coding for everyone
My logic is -
I created two data structure, array and map(hash table).
Array gives x-coordinate of ith frog.
And, I created map<int, set> or map<k, set{i}>, where k is x-coordinate and set contains ith index of frog which is reachable to k either direct or indirect.
Given, A and B - what I did, I tried to check, Is index ‘A’ present in set of map[B] ? that means, can frog at index ‘A’ reachable to coordinate of ‘B’ or not? In this manner I applied the condition.
Please help me to find out where is the problem. When I give sample input, it works fine.

I have gone through the code. Looks like you are merging the sets and storing that in only B. You should keep that in both map[a] and map[b]. If a can reach b then both can reach the same frogs

I tried printing map[5] for the example input it says it will reach 1,2,4 this is beacause you dont keep the merged set in map[a]. The correct set is 1,2,3,4

And this is not the best way to solve this question.

@echo_abhinav Thanks a lot! I got, where was the mistake.

And this is not the best way to solve this question.

I am trying to improve myself.