Given an undirected multigraph and required reachable pairs, decide for each edge whether every valid orientation matches the input direction (R), the reverse (L), or both are possible (B).
Hard9GraphDFSUnion-findImplementationNo attempts yetTime limit3sMemory limit256 MBA country has n cities and m two-way roads connecting them. Vehicles grew larger and faster, and the roads became too narrow for two vehicles travelling in opposite directions. The government decided to turn every road into a single-lane, one-way road.
Making the roads one-way has a cost. Some pairs of cities that used to be connected may no longer reach each other. The government wrote down a list of important pairs of cities, and for every pair on the list it must be possible to start in the first city and arrive at the second one. Decide the direction of traffic on every road. Only inputs that have a solution are given.
For some roads a solution leaves no choice of direction. Traffic that flows from the first city written in the input to the second one goes in the right direction, written R. Traffic that flows from the second city to the first one goes in the left direction, written L. Other roads have a solution with the road directed left and also a solution with the road directed right. Mark such roads with the letter B.
Print a string of length m. Its i-th character is
The first line contains the number of cities n and the number of roads m. Each of the next m lines describes one road with two city numbers ai and bi, meaning that a road connects city ai and city bi. Several roads may connect the same pair of cities, and a road may connect a city with itself.
The next line contains the number of city pairs p that have to be reachable. Each of the next p lines contains two city numbers xi and yi, meaning that it must be possible to start in city xi and arrive at city yi.
Print the string of length m described in the statement.
In the first example the fifth road, 1 3, can be directed either way. LLRLRL and RLRRLL are two solutions that differ in the direction of the fifth road.