Lining up Children
시간 제한1초메모리 제한1024 MB
N명의 아이와 M개의 친구 관계가 주어질 때, 모든 아이가 자신의 친구 옆에 서도록 줄을 세우는 순서를 찾거나 불가능하다고 판정한다.
문제
After a field trip, the teacher needs to line up all children in order to count them and verify that none are missing. Anyone who has ever had to deal with children knows how difficult this can be. A child absolutely must stand next to each of their friends, otherwise a riot will soon occur.
Find one possible way to order the children such that all children are satisfied.
입력
The first line of input contains two space-separated integers and ---the number of children and the number of pairs who are friends (, ).
The next line contains space-separated strings---the names of the children. Names are at most 10 symbols long and consist of only uppercase and lowercase Latin letters and dashes. It is guaranteed that each child has a name different from the rest.
Then follow lines, each containing two space-separated names, denoting a pair of friends.
출력
The first line of output should contain SAAB if a solution is possible, or EI SAA, if it is not. If a solution is possible, output on the second line the list of names in an order that would satisfy all children.