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

#FW0000037. Các nhóm gọi điện (Calling Circles)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Các nhóm gọi điện (Calling Circles)

Calling Circles

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Partition people into strongly connected calling circles: within a circle every person can reach every other via directed calls.

Input

Integers n,mn,m, followed by mm directed calls A,BA,B.

Output

Print each group on its own line with names separated by comma-space; sort names within each group and groups by their first name.

Subtasks

  • Subtask 1 (40 points): n≤8n\le8; 2≤n≤252\le n\le25, 1≤m≤4001\le m\le400; exactly nn distinct names appear; alphabetic names at most 25 characters.
  • Subtask 2 (60 points): 2≤n≤252\le n\le25, 1≤m≤4001\le m\le400; exactly nn distinct names appear; alphabetic names at most 25 characters.

Examples

Input

5 5
Ana Bao
Bao Chi
Chi Ana
Dao Em
Em Dao

Output

Ana, Bao, Chi
Dao, Em

Explanation

Ana, Bao, and Chi form one strongly connected component; Dao and Em form another. No pair from different components is mutually reachable.