Sets (Codeforces 82B)
Tags: Implement, hashing

Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose n non-empty sets in such a way, that no two of them have common elements.One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on n·(n - 1) / 2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.For example, if n = 4, and the actual sets have the following form {1, 3}, {5}, {2, 4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers:   2, 7, 4.  1, 7, 3;  5, 4, 2;  1, 3, 5;  3, 1, 2, 4;  5, 7. Then Vasya showed the pieces of paper to his friends, but kept the n sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?

Input: The first input file line contains a number n (2 ≤ n ≤ 200), n is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on n·(n - 1) / 2 lines. Each set starts with the number ki (2 ≤ ki ≤ 200), which is the number of numbers written of the i-th piece of paper, and then follow ki numbers aij (1 ≤ aij ≤ 200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from n non-intersecting sets.

Output: Print on n lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them.It is guaranteed that there is a solution.


Sample Inputs:
4
3 2 7 4
3 1 7 3
3 5 4 2
3 1 3 5
4 3 1 2 4
2 5 7

4
5 6 7 8 9 100
4 7 8 9 1
4 7 8 9 2
3 1 6 100
3 2 6 100
2 1 2

3
2 1 2
2 1 3
2 2 3

Sample Outputs:
1 7
2 2 4
2 1 3
1 5

3 7 8 9
2 6 100
1 1
1 2

1 1
1 2
1 3

Solution (Python):
n = int(input())
l = [set(input().split()[1:]) for _ in range(n * (n - 1) // 2)]
if n == 2:
    l = list(l[0])
    print(1, l[0])
    print(len(l) - 1, *l[1:])
q = set()
for i in range(n * (n - 1) // 2 - 1):
    for j in range(i + 1, n * (n - 1) // 2):
        p = l[i] & l[j]
        if p != set():
            q.add(' '.join(sorted(list(p))))
            q.add(' '.join(sorted(list(l[i] ^ p))))
            q.add(' '.join(sorted(list(l[j] ^ p))))
        if len(q) == n:
            break
    else:
        continue
    break
for i in q:
    if i != '':
        print(len(i.split()), i)

Solution (Python):
n = int(input())
L = []
for i in range(n * (n - 1) // 2):
    A = input().split()
    L.append(A[1:])
x = L[0][0]
Set = list(L[0])
Set.remove(x)
for i in range(1, n * (n - 1) // 2):
    if x in L[i]:
        for item in L[i]:
            if item in Set:
                Set.remove(item)
        break
x = Set[0]
Sets = []
Sets.append(list(Set))
for i in range(n * (n - 1) // 2):
    if x in L[i]:
        Sets.append(list(L[i]))
        for item in Set:
            Sets[-1].remove(item)
for item in Sets:
    print(len(item), end='')
    for z in item:
        print(' ' + str(z), end='')
    print()

Solution (Python):
n = int(input())
m = n * (n - 1) // 2
s = [set(t.split()[1:]) for t in [input() for i in range(m)]]
q = set(max([s[0], [t - s[0] for t in s[1:] if t & s[0]]][len(s) > 1]))
for t in [q] + [t - q for t in s if t & q]:
    print(len(t), ' '.join(t))