Memory Limit and TLE in Feasible relations (HACKEREARTH)

Problem Link: https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/feasible-relations/description/

Solution Link: https://ideone.com/ToMs5N

Getting Partial Score.
Getting MLE in 2 test cases and TLE in 1.

Can anyone please help me , how to optimize the code further

Since the constraints N, K are upto 1e6. So creating an adj list may give MLE.

You can try to solve the problem using Disjoint sets by merging the elements which are equal in same set and checking the validity in case of inequality bnm.

1 Like

ok thank you…I’ll Try