This page is still under construction.

Parts of this page are still being built. What you see may change.

Motorways

Time limit1sMemory limit128 MB

Summary
Assign each of k motorway chords to one of two sides so that no two chords on the same side interleave, choosing the lexicographically smallest assignment.
Level

Hard8 of 10

Topics
Graph, Sorting, Greedy
Solved
No attempts yet

Statement

Byteotia lies on a peninsula. Ever since the reign of King Byteol, railways have been the main means of transport there. King Byteol had a super-speed railway line built that connects the western and eastern coasts of the peninsula. It runs through every town of Byteotia and so fixes their numbering: the first town on the line is town 11 and the last is town nn. Town 11 lies on the western coast and town nn on the eastern coast.

The Byteotian railway line

Fig. 1. The Byteotian railway line.

Thanks to minister Byterowicz the economy has grown quickly, and the transport network must be modernised. King Byteol has ordered kk motorways to be built. Each motorway directly joins two chosen towns. Because every motorway is built by a different agency with its own vignette, no motorway may cross another motorway or the railway line. The only way to achieve this is to build each motorway either to the north or to the south of the railway line, drawn as an arc between its two towns.

A sample arrangement of motorways

Fig. 2. A sample arrangement of the motorways joining towns 1-2, 1-3, 2-4, 5-7, 4-8, 7-8, 6-8 (arcs are dotted, the railway line is solid).

Two motorways placed on the same side of the railway cross exactly when their town intervals interleave: exactly one endpoint of one motorway lies strictly between the two endpoints of the other. Motorways that share a town, that are nested one inside the other, or that are completely separate never cross and may share a side. Motorways on opposite sides never cross.

King Byteol has already fixed which pairs of towns are to be joined. Decide, for every motorway, whether it goes to the north or to the south of the railway line so that no two motorways cross, or report that no such placement exists.

Input

The first line contains two integers nn and kk (1≤n,k≤200001 \le n, k \le 20000): the number of towns and the number of planned motorways.

Each of the next kk lines contains two integers pip_i and qiq_i (1≤pi<qi≤n1 \le p_i < q_i \le n): the towns joined by the ii-th motorway. No pair of towns is repeated.

Output

If no valid placement exists, print a single line containing IMPOSSIBLE.

Otherwise print kk lines. The ii-th line contains one uppercase letter for the ii-th motorway (in input order): N if that motorway must be built to the north of the railway line, or S if to the south.

Several placements may be valid. Among all valid placements, print the lexicographically smallest one, reading the kk letters from top to bottom as a single string and treating N as smaller than S.

Examples6

  1. Example 1

    Input
    8 7
    1 2
    1 3
    2 4
    5 7
    4 8
    7 8
    6 8
    
    Expected output
    N
    N
    S
    N
    N
    N
    S
    
  2. Example 2

    Input
    2 1
    1 2
    
    Expected output
    N
    
  3. Example 3

    Input
    4 2
    1 3
    2 4
    
    Expected output
    N
    S
    
  4. Example 4

    Input
    4 2
    1 4
    2 3
    
    Expected output
    N
    N
    
  5. Example 5

    Input
    4 2
    1 2
    3 4
    
    Expected output
    N
    N
    
  6. Example 6

    Input
    6 3
    1 4
    2 5
    3 6
    
    Expected output
    IMPOSSIBLE