Solution to DEPEND problem (http://www.spoj.com/problems/DEPEND/)