If two trains of length a metres and b metres are moving in opposite directions at u m/s and v m/s, then :
The time taken by the trains to cross each other = (a + b)(u + v) sec

If two trains of length a metres and b metres are moving in same directions at u m/s and v m/s, then :
The time taken by the trains to cross each other = (a + b)(uv) sec

If two trains (or bodies) start at the same time from points A and B towards each other and after crossing they take a and b sec in reaching B and A respectively, then:
(A's speed) : (B's speed) = (b : a)