The first time the bugs met, they travelled the length of the branch, and the second time they met they travelled twice the length of the branch after their first meeting. So it took them each twice the distance to travel between the meetings. It follows that the first meeting point is 2*40-20 = 60 cm from the other end of the branch. Hence the branch is 40+60 = 100 cm long.
Solution 2.
Let x be the distance of the branch. Since the ratio of their speeds is constant, so the ratio of their travelled distances is also constant. Therefore, we have
40/(x-40) = [(x-40)+20]/[(x-20)+40],
40/(x-40) = (x-20)/(x+20),
40x + 800 = x^2-60x+800,
x^2-100x = 0,
x(x-100) = 0,
x = 100 cm.