railroad graph problem