Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 1)

class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size + 1))

    def is_same_set(self, x: int, y: int) -> bool:
        return self.find_root(x) == self.find_root(y)

    def union_set(self, x: int, y: int):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return

        self.parent[x] = y

    def find_root(self, x: int):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find_root(self.parent[x])
            return self.parent[x]


def dfs(n, used, edges, uf: UnionFind):
    used[n] = True
    for a in edges[n]:
        if not used[a]:
            uf.union_set(n, a)
            dfs(a, used, edges, uf)


def main():
    N, K, L = map(int, input().split())
    d1 = defaultdict(list)
    for _ in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        d1[p].append(q)
        d1[q].append(p)

    d2 = defaultdict(list)
    for _ in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        d2[r].append(s)
        d2[s].append(r)

    uf1 = UnionFind(N)
    used1 = [False] * N
    for n in range(N):
        if not used1[n]:
            dfs(n, used1, d1, uf1)

    uf2 = UnionFind(N)
    used2 = [False] * N
    for n in range(N):
        if not used2[n]:
            dfs(n, used2, d2, uf2)

    used3 = [False] * N
    ans = [0] * N
    for n1 in range(N):
        if used3[n1]:
            continue
        num = 1
        l = [n1]
        for n2 in range(n1 + 1, N):
            if not used3[n2]:
                if uf1.is_same_set(n1, n2) and uf2.is_same_set(n1, n2):
                    used3[n2] = True
                    l.append(n2)
                    num += 1
        for n in l:
            ans[n] = num

    print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main()


Fixed submission:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 5 + 1)


class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size))

    def is_same_set(self, x: int, y: int) -> bool:
        return self.find_root(x) == self.find_root(y)

    def union_set(self, x: int, y: int):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return

        self.parent[x] = y

    def find_root(self, x: int):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find_root(self.parent[x])
            return self.parent[x]


def main():
    N, K, L = map(int, input().split())
    uf1 = UnionFind(N)
    for _ in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        uf1.union_set(p, q)

    uf2 = UnionFind(N)
    for _ in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        uf2.union_set(r, s)

    groups = [(uf1.find_root(i), uf2.find_root(i)) for i in range(N)]
    c = Counter(groups)

    print(*(c[g] for g in groups))


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict


class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size + 1))

    def is_same_set(self, x: int, y: int) -> bool:
        return self.find_root(x) == self.find_root(y)

    def union_set(self, x: int, y: int):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return

        self.parent[x] = y

    def find_root(self, x: int):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find_root(self.parent[x])
            return self.parent[x]


def dfs(n, used, edges, uf: UnionFind):
    used[n] = True
    for a in edges[n]:
        if not used[a]:
            uf.union_set(n, a)
            dfs(a, used, edges, uf)


def main():
    N, K, L = map(int, input().split())
    d1 = defaultdict(list)
    for _ in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        d1[p].append(q)
        d1[q].append(p)

    d2 = defaultdict(list)
    for _ in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        d2[r].append(s)
        d2[s].append(r)

    uf1 = UnionFind(N)
    used1 = [False] * N
    for n in range(N):
        if not used1[n]:
            dfs(n, used1, d1, uf1)

    uf2 = UnionFind(N)
    used2 = [False] * N
    for n in range(N):
        if not used2[n]:
            dfs(n, used2, d2, uf2)

    used3 = [False] * N
    ans = [0] * N
    for n1 in range(N):
        if used3[n1]:
            continue
        num = 1
        l = [n1]
        for n2 in range(n1 + 1, N):
            if not used3[n2]:
                if uf1.is_same_set(n1, n2) and uf2.is_same_set(n1, n2):
                    used3[n2] = True
                    l.append(n2)
                    num += 1
        for n in l:
            ans[n] = num

    print(" ".join(map(str, ans)))


if __name__ == '__main__':
    main()


Fixed submission:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 5 + 1)


class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size))

    def is_same_set(self, x: int, y: int) -> bool:
        return self.find_root(x) == self.find_root(y)

    def union_set(self, x: int, y: int):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return

        self.parent[x] = y

    def find_root(self, x: int):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find_root(self.parent[x])
            return self.parent[x]


def main():
    N, K, L = map(int, input().split())
    uf1 = UnionFind(N)
    for _ in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        uf1.union_set(p, q)

    uf2 = UnionFind(N)
    for _ in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        uf2.union_set(r, s)

    groups = [(uf1.find_root(i), uf2.find_root(i)) for i in range(N)]
    c = Counter(groups)

    print(*(c[g] for g in groups))


if __name__ == '__main__':
    main()