They both are 40, correct?
Then shouldn't it be a tie?
Yellow's "longest train" is only 38 points. Loops won't count towards the "longest train" count unless they can be continued without doubling over itself. Otherwise, only the longest branch will be counted.
So, taking a segment of your example:
Here, the segment would be worth 11 points without counting the extra two trains. This is better than the alternative where the longest train tries to follow the loop:
This segment is only worth 9 points total. It tries to follow the loop but has no way to continue without doubling over existing track, so it has to terminate at Dallas. The only way the whole loop could be counted is if the track can continue after completing it:
In this case, I've added a hypothetical extension off of Dallas: This way all the trains in the loop could all be counted.
Green appears to have a length of 40 and Yellow appears to have a length of 38.
Looking at the green path there are a total of 4 cars that can't be counted in the longest path. Sault St. Marie to Toronto and New York to Pittsburgh.
Looking at the yellow path and there are a total of 5 cars that can't be counted. Dallas to Oklahoma City and Denver to Salt Lake City.
In order to qualify for the longest path it needs to be a single path with no branches and never doubling back on itself.