PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000060. Phụ thuộc tệp nguồn (Import Spaghetti)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phụ thuộc tệp nguồn (Import Spaghetti)

Import Spaghetti

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Some source files import other files, forming a directed dependency graph. Find a directed cycle with the fewest edges and print its file names in cyclic order without repeating its first name. A self-import is a cycle of length one.

Input

First n m; then n distinct file names; then m directed dependencies sourceFile importedFile.

Output

Print exactly SHIP IT if acyclic; otherwise print any shortest directed cycle as space-separated file names, without repeating the first name.

Subtasks

  • Subtask 1 (40 points): 1≤n≤8, 0≤m≤n21\le n\le 8,\ 0\le m\le n^2. File names are distinct alphanumeric tokens. Each ordered import edge appears at most once; a file may import itself.
  • Subtask 2 (60 points): 1≤n≤100, 0≤m≤n21\le n\le 100,\ 0\le m\le n^2. File names are distinct alphanumeric tokens. Each ordered import edge appears at most once; a file may import itself.

Examples

Input

3 3
a b c
a b
b c
c a

Output

a b c

Explanation

Dependencies a→b→c→a form a three-file cycle. No shorter cycle exists; any rotation of a b c is accepted.