General (SPOJ - CHAIN)

Problem:

Please, someone, explain how to solve this problem, and don’t redirect me to Quora link
I couldn’t get it from there.

https://www.quora.com/How-do-I-solve-the-strange-food-chain-question-on-SPOJ