Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k, l = map(int, input().split())
    roads = [set([i]) for i in range(n)]
    rails = [set([i]) for i in range(n)]
    for _ in range(k):
        a, b = map(lambda s: int(s) - 1, input().split())
        i_large, i_small = (a, b) if len(roads[a]) > len(roads[b]) else (b, a)
        roads[i_large] |= roads[i_small]
        for i in roads[i_small]:
            roads[i] = roads[i_large]
    for _ in range(l):
        a, b = map(lambda s: int(s) - 1, input().split())
        i_large, i_small = (a, b) if len(rails[a]) > len(rails[b]) else (b, a)
        rails[i_large] |= rails[i_small]
        for i in rails[i_small]:
            rails[i] = rails[i_large]
    res = [len(roads[i] & rails[i]) for i in range(n)]
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()


Fixed submission:
def root(parents, i):
    if parents[i] == i:
        return i
    parents[i] = root(parents, parents[i])
    return parents[i]


def main():
    n, k, l = map(int, input().split())
    roads = list(range(n))
    rails = list(range(n))

    for _ in range(k):
        a, b = map(lambda s: int(s) - 1, input().split())
        root_a, root_b = root(roads, a), root(roads, b)
        if root_a != root_b:
            roads[root_a] = root_b

    for _ in range(l):
        a, b = map(lambda s: int(s) - 1, input().split())
        root_a, root_b = root(rails, a), root(rails, b)
        if root_a != root_b:
            rails[root_a] = root_b

    counts = dict()
    for i in range(n):
        pair = (root(roads, i), root(rails, i))
        if not pair in counts:
            counts[pair] = 1
        else:
            counts[pair] += 1

    print(' '.join(
        map(lambda i: str(counts[(root(roads, i), root(rails, i))]), range(n))))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k, l = map(int, input().split())
    roads = [set([i]) for i in range(n)]
    rails = [set([i]) for i in range(n)]
    for _ in range(k):
        a, b = map(lambda s: int(s) - 1, input().split())
        u = roads[a] | roads[b]
        for i in u:
            roads[i] = u
    for _ in range(l):
        a, b = map(lambda s: int(s) - 1, input().split())
        u = rails[a] | rails[b]
        for i in u:
            rails[i] = u
    res = [len(roads[i] & rails[i]) for i in range(n)]
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()


Fixed submission:
def root(parents, i):
    if parents[i] == i:
        return i
    parents[i] = root(parents, parents[i])
    return parents[i]


def main():
    n, k, l = map(int, input().split())
    roads = list(range(n))
    rails = list(range(n))

    for _ in range(k):
        a, b = map(lambda s: int(s) - 1, input().split())
        root_a, root_b = root(roads, a), root(roads, b)
        if root_a != root_b:
            roads[root_a] = root_b

    for _ in range(l):
        a, b = map(lambda s: int(s) - 1, input().split())
        root_a, root_b = root(rails, a), root(rails, b)
        if root_a != root_b:
            rails[root_a] = root_b

    counts = dict()
    for i in range(n):
        pair = (root(roads, i), root(rails, i))
        if not pair in counts:
            counts[pair] = 1
        else:
            counts[pair] += 1

    print(' '.join(
        map(lambda i: str(counts[(root(roads, i), root(rails, i))]), range(n))))


if __name__ == '__main__':
    main()