APAC google code Jam contest

An undirected graph is constructed to derive the solution. Though seems
to work with the sample, it is not working on either small or large set.
Especially, the programs runs a lot slower in the large set.