A Series of Tubes
InterviewTime limit1sMemory limit512 MB
Given a connected undirected graph, decide whether its edges can be oriented so that the result stays strongly connected.
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 and the number of fiber branches , separated by one space (, ). The capitals are numbered 1 through .
Each of the next lines has two integers and (, ), meaning a branch directly joins capital and capital . For data traffic this branch works from to and from to , 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 and 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.