LearnPy Hub
← Tuples & Sets problems
Hard

Friend Groups

Read n pairs of friends. Friends of friends are in the same group. Build groups as sets, merging sets when a pair links two groups. Print the number of groups, then each group's members sorted, groups ordered by their first member.

Input

n, then n lines of two names.

Output

Lines.

Sample 1

Input
3
Mim Rafi
Sara Tom
Rafi Nadia
Output
2
Mim Nadia Rafi
Sara Tom

Sample 2

Input
1
A B
Output
1
A B
solution.py
Output appears here
Friend Groups · Python practice problem · LearnPy Hub