Under construction

Still being built. Things may change

A Series of Tubes

Interview

Time limit1sMemory limit512 MB

Summary
Given a connected undirected graph, decide whether its edges can be oriented so that the result stays strongly connected.
Level

Medium4 of 10

Topics
Graph, DFS
Solved
No attempts yet

Problem

A company has developed a way to send matter through optical fiber cables. It already owns a fiber network that reaches every state capital in the country, and it wants to move cargo over that network. The old joke that the internet is not a truck you dump things into but a series of tubes turned out to be literally useful.

The network is made of fiber branches and concentrators. Each capital holds one concentrator, and one fiber branch directly joins two concentrators. Not every pair of concentrators is joined by a branch, but the network is connected: starting from any concentrator you can reach any other concentrator by following branches and concentrators in turn.

For data traffic a branch carries messages in both directions. The new technology has a quirk. Once a branch carries matter in one direction, the fiber remembers that direction, and from then on the branch can carry matter only that way. Concentrators are not affected by this memory.

Write a program that decides whether a direction can be fixed on every branch so that cargo can still be sent from any capital to any other capital.

Input

The input holds several test cases.

The first line of a test case has the number of capitals NN and the number of fiber branches MM, separated by one space (2≤N≤10002 \le N \le 1000, 1≤M≤500001 \le M \le 50000). The capitals are numbered 1 through NN.

Each of the next MM lines has two integers AA and BB (1≤A,B≤N1 \le A, B \le N, A≠BA \ne B), meaning a branch directly joins capital AA and capital BB. For data traffic this branch works from AA to BB and from BB to AA, but for matter transfer it works in one direction only. At most one branch joins a given pair of capitals. The network of each test case is connected.

The end of the input is a line where NN and MM are both 0. That line is not a test case.

Output

Print one line for each test case. Print S if the network can be used as described, and N otherwise.

Examples3

  1. Example 1

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

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

    Input
    3 3
    1 2
    2 3
    3 1
    0 0
    
    Expected output
    S