SPOJ: Is it a tree. Solved this after many many silly mistakes.