The official test data from http://www.usaco.org/index.php?page=viewproblem2&cpid=102 contains a case (namely, I.9) that has a disconnected graph -- the graph is not a tree. What are we supposed to do? I get the correct output for all the test cases except the 9th, and consequently SPOJ's verdict is TLE after disabled asserts, and RTE with asserts enabled.
Never mind, got Accepted. The official data is correct, I was wrong.