Feasible Relations

hackerearth
union-find

#1

Feasible Relations

Solution Link

I have used Union-find data structure and the basic idea is to first process all queries having ‘=’ and then processing all queries with “!=”. I am getting SIGSEGV error For 4 out of 7 testcases could anybody tell where am i going wrong???


#2

Your approach is correct. The problem lies in the if block on line 75

if(uf.root(a)==uf.root(b))
{
    ans="NO";
    break;
}

When this condition is triggered you are breaking out the loop where you are taking input. So you’re moving onto the next test case without completely reading the inputs of the current test case. This naturally causes the actual leftover input to differ from the expected format which results in runtime error.

Just remove the if block and your code runs fine, since you are checking all "!=" cases later anyway from the qu vector! :slight_smile:


#3

i am unaware about the oop memory related concepts in c++ so is it due to them


#4

awesome thanks it worked!! :slight_smile: