Help for SEGSEGV error ( In Brute force simple method ) in XORMIN

problem statement : CodeChef: Practical coding for everyone
my solution : CodeChef: Practical coding for everyone

Although my code is purely inefficient ( simple brute force ) , but I wanted to know the reason for segmentation error ?
also why I m not getting segmentation fault in all tasks …. why only in some test cases ?

U got 10pts due to weak test cases on Subtask 1. Let’s have a look at problem statement:-

The answer to this query is the maximum of these values and the smallest u such that vertex u is in the subtree of vertex v and w_u⊕k is equal to this maximum.

You code isn’t finding the smallest u for the max value, it’s printing any u with max value.

Reason for ur getting segmentation faults is, cause if 1 of ur query gets incorrect, then it’d lead to making of a different query for next case. Which may also lead to out of bounds query at some instance.

In ur dfs function, in this particular block:-

if( val > ans)
{
    ans = val;
    va = v;
}

, I’ve just added these 3 lines:-

else if(val==ans)
{
    va=min(v,va);
}

to make ur ans correct as per problem statement :wink:

Thank you so much for figuring out the problem !!