Official

B - Gift Editorial by en_translator


If person \(i\) sent a gift to person \(A_{i,j}\), that means person \(A_{i,j}\) received a gift from person \(i\). Thus, it is sufficient to track who received a gift from whom, and print the accumulated results.

Sample code (Python 3)

n = int(input())
ans = [[] for _ in range(n)]
for i in range(n):
    a = list(map(int, input().split()))[1:]
    for c in a:
        ans[c - 1].append(i + 1)
for i in range(n):
    a = [len(ans[i])] + ans[i]
    print(*a)

posted:
last update: