Under construction

Still being built. Things may change

Brightline - Back to the Future

Interview

Time limit2sMemory limit1024 MB

Summary
Given directed blue lines (aging riders) and red lines (rewinding riders), list every city reachable from city 1 where a rider arrives younger than they started.
Level

Medium5 of 10

Topics
Shortest path, Graph
Solved
No attempts yet

Problem

Many UCF programming team coaches are getting old and want any way to regain their youth. A new train, the Brightline, has arrived in town. Rumor has it that a person who rides some of the trains can get younger.

Brightline runs two types of trains: the blue line and the red line. On a blue line, passengers age by a set amount of time. On a red line, passengers go back in time by a set amount of time.

Each line is directed. It connects a source city to a destination city, is labeled red or blue, and has a positive integer giving the number of minutes a passenger ages (blue) or moves back in time (red) when taking that line. Whenever you arrive in a city, you can immediately take any train leaving that city.

The system does not allow unlimited travel back in time. There is no way to start at any city and return to that same city younger than you were when you started.

All of the coaches live in Orlando, which is city 1. Given the list of all Brightline lines, find every destination city that someone starting in city 1 can reach while being younger than when they started.

Input

The first line contains two integers nn (1≤n≤5×1031 \le n \le 5 \times 10^3) and mm (1≤m≤1041 \le m \le 10^4). The cities are numbered 1 to nn, where city 1 is Orlando.

Each of the next mm lines describes one train line with four values:

  • ss (1≤s≤n1 \le s \le n): the starting city of the line
  • ee (1≤e≤n1 \le e \le n, s≠es \ne e): the ending city of the line
  • tt (t=t = 'b' or 'r'): 'b' for a blue line, 'r' for a red line
  • aa (1≤a≤100001 \le a \le 10000): the number of minutes one ages (blue) or moves back in time (red) on this line

Output

Print each destination city that someone starting in city 1 can reach while being younger than when they started, in increasing numerical order.

Examples2

  1. Example 1

    Input
    5 8
    1 2 b 7
    1 3 b 3
    3 2 b 4
    2 4 r 2
    3 5 r 5
    5 2 b 1
    4 3 b 6
    5 4 b 4
    
    Expected output
    2
    4
    5
    
  2. Example 2

    Input
    5 9
    1 2 b 3
    1 5 r 4
    1 3 b 8
    2 4 b 1
    2 5 b 7
    3 2 b 4
    4 1 b 2
    4 3 r 5
    5 4 b 6
    
    Expected output
    3
    5