Lining up Children

시간 제한1초메모리 제한1024 MB

요약
N명의 아이와 M개의 친구 관계가 주어질 때, 모든 아이가 자신의 친구 옆에 서도록 줄을 세우는 순서를 찾거나 불가능하다고 판정한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 구현
정답자
아직 제출이 없습니다

문제

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 NN and MM---the number of children and the number of pairs who are friends (1≤N≤1051 \le N \le 10^5, 1≤M≤3⋅1051 \le M \le 3 \cdot 10^5).

The next line contains NN 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 MM 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.

예제2

  1. 예제 1

    입력
    5 3
    Bob Carol Eve Dave Alice
    Alice Carol
    Alice Bob
    Dave Eve
    
    예상 출력
    SAAB
    Bob Alice Carol Eve Dave
    
  2. 예제 2

    입력
    3 3
    Regina Gretchen Karen
    Regina Gretchen
    Gretchen Karen
    Regina Karen
    
    예상 출력
    EI SAA