CCCGuide

Problem S4: Painting Roads

2024 · S4 · Original problem · DMOJ

Problem Description

Alanna, the mayor of Kitchener, has successfully improved the city’s road plan. However, a travelling salesperson from the city of RedBlue complained that the roads are not colourful enough. Alanna’s second job is to paint some of the roads.

Kitchener’s road plan can be represented as a collection of NN intersections with MM roads, where the ii-th road connects intersections uiu_i and viv_i. All roads are initially grey. Alanna would like to paint some of the roads in red or blue such that the following condition is satisfied:

To lower the city’s annual spending, Alanna would like to minimize the number of painted roads. Can you help Alanna design a plan that meets all the requirements?

Input Specification

The first line contains two integers NN and MM (1≤N,M≤2⋅105)(1 \le N,M \le 2 \cdot 10^5).

The ii-th of the next MM lines contains two integers uiu_i and viv_i, meaning that there exists a road from intersection uiu_i to intersection viv_i (1≤ui,vi≤N,ui≠vi)(1 \le u_i,v_i \le N, u_i \ne v_i).

There is at most one road between any unordered pair of intersections.

The following table shows how the available 15 marks are distributed:

MarksAdditional Constraints
22There is a road connecting intersection ii with intersection i+1i+1 for all 1≤i<N1 \le i < N (and possibly other roads).
33We can reach any intersection from any other intersection, and N=MN=M.
33No road belongs to two or more simple cycles (see Definition below).
77None

Definition: if we denote a road between intersections uu and vv as u↔vu \leftrightarrow v, then a simple cycle is a sequence w1↔w2↔…↔wk↔w1w_1 \leftrightarrow w_2\leftrightarrow \ldots \leftrightarrow w_k \leftrightarrow w_1 where k≥3k \geq 3 and all wiw_i are distinct.

Output Specification

Output a string of MM characters, representing the paint plan. The ii-th character should be R if the ii-th road is to be painted red, B if ii-th road is to be painted blue, or G (for "grey") if the ii-th road is to be left unpainted.

Remember that you must minimize the number of painted roads while satisfying the condition. If there are multiple possible such plans, output any of them.

Sample Input 1

5 7
1 2
2 4
5 2
4 5
4 3
1 3
1 4

Output for Sample Input 1

RGGRGRB

Explanation of Output for Sample Input 1

A diagram of the intersections along with a valid paint plan that minimizes the number of painted roads is shown below. Note that the colours are shown on each road as R (red), B (blue), or G (grey).

Circles labelled 1 through 5 connected by coloured lines. The pairs 1 and 2, 1 and 3, and 4 and 5 are connected by red lines. The pair 1 and 4 is connected by a blue line. The pairs 2 and 4, 2 and 5, and 3 and 4 are connected by grey lines.

All the unpainted roads satisfy the condition:

Sample Input 2

4 2
1 2
3 4

Output for Sample Input 2

BB

Explanation of Output for Sample Input 2

Note that it is possible for Kitchener to be disconnected.