Little Shino and Kth Ancestor

Question link-https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/little-shino-and-k-ancestor-57fdef57/
Code Link-https://ide.codingblocks.com/s/334843
Many test cases are failing ,Help!!

I think you did not get the question correctly.

The question asks for “the kth ancestor with the same color” and not “if the kth ancestor has the same color”.