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]

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 :