How to solve IOPC15L problem ?

I know its based on basic graph algorithm but i am not able to come with the idea to solve it?

[Problem link ][1]
[1]: CodeChef: Practical coding for everyone

For every a,b given, i made a directed edge from a to b. This ensures all our initially filled nodes are at the bottom. Now from every point do a dfs and add the probability of the individual colours.

Code : http://www.codechef.com/viewplaintext/6514583