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]: http://www.codechef.com/IOPC2015/problems/IOPC15L

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]: http://www.codechef.com/IOPC2015/problems/IOPC15L

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.