Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
from collections import defaultdict

def main():
    N, K, L = (int(_) for _ in input().split())
    road = defaultdict(list)
    train = defaultdict(list)
    for i in range(K):
        p, q = (int(_) for _ in input().split())
        road[p].append(q)
        road[q].append(p)
    for i in range(L):
        p, q = (int(_) for _ in input().split())
        train[p].append(q)
        train[q].append(p)
    visited_r = [0] * (N+1)
    gn = 1
    for p in range(1, N+1):
        if visited_r[p] != 0:
            continue
        d = deque()
        d.append(p)
        while d:
            q = d.popleft()
            visited_r[q] = gn
            if q in road:
                for r in road[q]:
                    if visited_r[r] == 0:
                        d.append(r)
        gn += 1
    visited_t = [0] * (N+1)
    gn = 1
    for p in range(1, N+1):
        if visited_t[p] != 0:
            continue
        d = deque()
        d.append(p)
        while d:
            q = d.popleft()
            visited_t[q] = gn
            if q in train:
                for r in train[q]:
                    if visited_t[r] == 0:
                        d.append(r)
        gn += 1
    dict = defaultdict(int)
    for i in range(1, N+1):
        dict[(visited_r[i], visited_t[i])] += 1
    for i in range(1, N+1):
        print(dict[(visited_r[i], visited_t[i])], end=' ')
    print('')
    return

if __name__ == '__main__':
    main()


Fixed submission:
#Union-Find木の構築
import sys
def 解():
    iN,iK,iL = [int(_) for _ in input().split()]
    aD = [[int(_) for _ in sLine.split()] for sLine in sys.stdin.readlines()]

    def buildTree(a):
        #aT = [0]*(iN+1)
        aT = list(range(iN+1))
        for p,q in a:
            rp = getRoot(aT,p)
            rq = getRoot(aT,q)
            thisRoot = max(rp,rq,q) #みんな小さい方に合わせてるのでデカい方に合>わせてみる
            aT[p] = thisRoot
            aT[q] = thisRoot
            aT[rp] = thisRoot #これで Union してるつもり
            aT[rq] = thisRoot
        return aT
    def getRoot(a,i): #find
        #if a[i] == i or a[i] == 0:
        #   if a[i] == 0 : a[i] = i
        if a[i] == i :
            return i
    #   elif i < a[i] :
        else :
            a[i] = getRoot(a,a[i])
            return a[i]
    #   else:
    #       print("! ",i,a[i])

    aT道 = buildTree(aD[0:iK])
    aT鉄道 = buildTree(aD[iK:])
    aRet = []
    dRet = {}
    for i in range(1,iN+1):
        # いやはやこんなもんもキーになるのか。
        Comb = (getRoot(aT道,i), getRoot(aT鉄道,i))
        aRet.append(Comb)
        if Comb in dRet:
            dRet[Comb] += 1
        else:
            dRet[Comb] = 1
    print(" ".join([str(dRet[_]) for _ in aRet]))

解()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    N, K, L = (int(_) for _ in input().split())
    road = {}
    train = {}
    for i in range(K):
        p, q = (int(_) for _ in input().split())
        if p in road:
            road[p].append(q)
        else:
            road[p] = [q]
        if q in road:
            road[q].append(p)
        else:
            road[q] = [p]
    for i in range(L):
        p, q = (int(_) for _ in input().split())
        if p in train:
            train[p].append(q)
        else:
            train[p] = [q]
        if q in train:
            train[q].append(p)
        else:
            train[q] = [p]
    visited_r = [0] * (N+1)
    gn = 1
    for p in range(1, N+1):
        if visited_r[p] != 0:
            continue
        d = deque()
        d.append(p)
        while d:
            q = d.popleft()
            visited_r[q] = gn
            if q in road:
                for r in road[q]:
                    if visited_r[r] == 0:
                        d.append(r)
        gn += 1
    visited_t = [0] * (N+1)
    gn = 1
    for p in range(1, N+1):
        if visited_t[p] != 0:
            continue
        d = deque()
        d.append(p)
        while d:
            q = d.popleft()
            visited_t[q] = gn
            if q in train:
                for r in train[q]:
                    if visited_t[r] == 0:
                        d.append(r)
        gn += 1
    dict = {}
    for i in range(1, N+1):
        if (visited_r[i], visited_t[i]) in dict:
            dict[(visited_r[i], visited_t[i])] += 1
        else:
            dict[(visited_r[i], visited_t[i])] = 1
    for i in range(1, N+1):
        print(dict[(visited_r[i], visited_t[i])], end=' ')
    print('')
    return

if __name__ == '__main__':
    main()


Fixed submission:
#Union-Find木の構築
import sys
def 解():
    iN,iK,iL = [int(_) for _ in input().split()]
    aD = [[int(_) for _ in sLine.split()] for sLine in sys.stdin.readlines()]

    def buildTree(a):
        #aT = [0]*(iN+1)
        aT = list(range(iN+1))
        for p,q in a:
            rp = getRoot(aT,p)
            rq = getRoot(aT,q)
            thisRoot = max(rp,rq,q) #みんな小さい方に合わせてるのでデカい方に合>わせてみる
            aT[p] = thisRoot
            aT[q] = thisRoot
            aT[rp] = thisRoot #これで Union してるつもり
            aT[rq] = thisRoot
        return aT
    def getRoot(a,i): #find
        #if a[i] == i or a[i] == 0:
        #   if a[i] == 0 : a[i] = i
        if a[i] == i :
            return i
    #   elif i < a[i] :
        else :
            a[i] = getRoot(a,a[i])
            return a[i]
    #   else:
    #       print("! ",i,a[i])

    aT道 = buildTree(aD[0:iK])
    aT鉄道 = buildTree(aD[iK:])
    aRet = []
    dRet = {}
    for i in range(1,iN+1):
        # いやはやこんなもんもキーになるのか。
        Comb = (getRoot(aT道,i), getRoot(aT鉄道,i))
        aRet.append(Comb)
        if Comb in dRet:
            dRet[Comb] += 1
        else:
            dRet[Comb] = 1
    print(" ".join([str(dRet[_]) for _ in aRet]))

解()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
def main():
    N, K, L = (int(_) for _ in input().split())
    road = {}
    train = {}
    for i in range(K):
        p, q = (int(_) for _ in input().split())
        if p in road:
            road[p].append(q)
        else:
            road[p] = [q]
        if q in road:
            road[q].append(p)
        else:
            road[q] = [p]
    for i in range(L):
        p, q = (int(_) for _ in input().split())
        if p in train:
            train[p].append(q)
        else:
            train[p] = [q]
        if q in train:
            train[q].append(p)
        else:
            train[q] = [p]
    visited_r = [0] * (N+1)
    gn = 1
    for p in range(1, N+1):
        if visited_r[p] != 0:
            continue
        d = deque()
        d.append(p)
        while d:
            q = d.popleft()
            visited_r[q] = gn
            if q in road:
                for r in road[q]:
                    if visited_r[r] == 0:
                        d.append(r)
        gn += 1
    visited_t = [0] * (N+1)
    gn = 1
    for p in range(1, N+1):
        if visited_t[p] != 0:
            continue
        d = deque()
        d.append(p)
        while d:
            q = d.popleft()
            visited_t[q] = gn
            if q in train:
                for r in train[q]:
                    if visited_t[r] == 0:
                        d.append(r)
        gn += 1
    dict = {}
    for i in range(1, N+1):
        if (visited_r[i], visited_t[i]) in dict:
            dict[(visited_r[i], visited_t[i])] += 1
        else:
            dict[(visited_r[i], visited_t[i])] = 1
    for i in range(1, N+1):
        print(dict[(visited_r[i], visited_t[i])], end=' ')
    print('')
    return

if __name__ == '__main__':
    main()


Fixed submission:
#Union-Find木の構築
import sys
def 解():
    iN,iK,iL = [int(_) for _ in input().split()]
    aD = [[int(_) for _ in sLine.split()] for sLine in sys.stdin.readlines()]

    def buildTree(a):
        #aT = [0]*(iN+1)
        aT = list(range(iN+1))
        for p,q in a:
            rp = getRoot(aT,p)
            rq = getRoot(aT,q)
            thisRoot = max(rp,rq,q) #みんな小さい方に合わせてるのでデカい方に合>わせてみる
            aT[p] = thisRoot
            aT[q] = thisRoot
            aT[rp] = thisRoot #これで Union してるつもり
            aT[rq] = thisRoot
        return aT
    def getRoot(a,i): #find
        #if a[i] == i or a[i] == 0:
        #   if a[i] == 0 : a[i] = i
        if a[i] == i :
            return i
    #   elif i < a[i] :
        else :
            a[i] = getRoot(a,a[i])
            return a[i]
    #   else:
    #       print("! ",i,a[i])

    aT道 = buildTree(aD[0:iK])
    aT鉄道 = buildTree(aD[iK:])
    aRet = []
    dRet = {}
    for i in range(1,iN+1):
        # いやはやこんなもんもキーになるのか。
        Comb = (getRoot(aT道,i), getRoot(aT鉄道,i))
        aRet.append(Comb)
        if Comb in dRet:
            dRet[Comb] += 1
        else:
            dRet[Comb] = 1
    print(" ".join([str(dRet[_]) for _ in aRet]))

解()

Buggy python submission (verdict: Runtime Error):
N, K, L = [int(x) for x in input().split()]
dict = {}
for i in range(1, N + 1):
    dict[i] = [set(), set()]
    dict[i][0].add(i)
    dict[i][1].add(i)
for i in range(K):
    p, q = [int(x) for x in input().split()]
    dict[p][0].add(q)
    dict[q][0].add(p)
for i in range(L):
    r, s = [int(x) for x in input().split()]
    dict[r][1].add(s)
    dict[s][1].add(r)

def my_dfs(city, opt, s):
    if dict[city][opt] <= s:
        return s
    for i in dict[city][opt]:
        if i in s:
            continue
        if i == city:
            s.add(i)
            continue
        s.add(i)
        s.update(my_dfs(i, opt, s))
    return s

for i in range(1, N + 1):
    dict[i][0].update(my_dfs(i, 0, set()))
    dict[i][1].update(my_dfs(i, 1, set()))
ans_lst = []
for i in range(1, N + 1):
    ans_lst.append(len(dict[i][0] & dict[i][1]))
print(*ans_lst)


Fixed submission:
#Union-Find木の構築
import sys
def 解():
    iN,iK,iL = [int(_) for _ in input().split()]
    aD = [[int(_) for _ in sLine.split()] for sLine in sys.stdin.readlines()]

    def buildTree(a):
        #aT = [0]*(iN+1)
        aT = list(range(iN+1))
        for p,q in a:
            rp = getRoot(aT,p)
            rq = getRoot(aT,q)
            thisRoot = max(rp,rq,q) #みんな小さい方に合わせてるのでデカい方に合>わせてみる
            aT[p] = thisRoot
            aT[q] = thisRoot
            aT[rp] = thisRoot #これで Union してるつもり
            aT[rq] = thisRoot
        return aT
    def getRoot(a,i): #find
        #if a[i] == i or a[i] == 0:
        #   if a[i] == 0 : a[i] = i
        if a[i] == i :
            return i
    #   elif i < a[i] :
        else :
            a[i] = getRoot(a,a[i])
            return a[i]
    #   else:
    #       print("! ",i,a[i])

    aT道 = buildTree(aD[0:iK])
    aT鉄道 = buildTree(aD[iK:])
    aRet = []
    dRet = {}
    for i in range(1,iN+1):
        # いやはやこんなもんもキーになるのか。
        Comb = (getRoot(aT道,i), getRoot(aT鉄道,i))
        aRet.append(Comb)
        if Comb in dRet:
            dRet[Comb] += 1
        else:
            dRet[Comb] = 1
    print(" ".join([str(dRet[_]) for _ in aRet]))

解()

Buggy python submission (verdict: Runtime Error):
N, K, L = [int(x) for x in input().split()]
dict = {}
for i in range(1, N + 1):
    dict[i] = [set(), set()]
    dict[i][0].add(i)
    dict[i][1].add(i)
for i in range(K):
    p, q = [int(x) for x in input().split()]
    dict[p][0].add(q)
    dict[q][0].add(p)
for i in range(L):
    r, s = [int(x) for x in input().split()]
    dict[r][1].add(s)
    dict[s][1].add(r)

def my_dfs(city, opt, s):
    if dict[city][opt] <= s:
        return s
    for i in dict[city][opt]:
        if i in s:
            continue
        if i == city:
            s.add(i)
            continue
        s.add(i)
        s.update(my_dfs(i, opt, s))
    return s

for i in range(1, N + 1):
    dict[i][0].update(my_dfs(i, 0, set()))
    dict[i][1].update(my_dfs(i, 1, set()))
ans_lst = []
for i in range(1, N + 1):
    ans_lst.append(len(dict[i][0] & dict[i][1]))
print(ans_lst)


Fixed submission:
#Union-Find木の構築
import sys
def 解():
    iN,iK,iL = [int(_) for _ in input().split()]
    aD = [[int(_) for _ in sLine.split()] for sLine in sys.stdin.readlines()]

    def buildTree(a):
        #aT = [0]*(iN+1)
        aT = list(range(iN+1))
        for p,q in a:
            rp = getRoot(aT,p)
            rq = getRoot(aT,q)
            thisRoot = max(rp,rq,q) #みんな小さい方に合わせてるのでデカい方に合>わせてみる
            aT[p] = thisRoot
            aT[q] = thisRoot
            aT[rp] = thisRoot #これで Union してるつもり
            aT[rq] = thisRoot
        return aT
    def getRoot(a,i): #find
        #if a[i] == i or a[i] == 0:
        #   if a[i] == 0 : a[i] = i
        if a[i] == i :
            return i
    #   elif i < a[i] :
        else :
            a[i] = getRoot(a,a[i])
            return a[i]
    #   else:
    #       print("! ",i,a[i])

    aT道 = buildTree(aD[0:iK])
    aT鉄道 = buildTree(aD[iK:])
    aRet = []
    dRet = {}
    for i in range(1,iN+1):
        # いやはやこんなもんもキーになるのか。
        Comb = (getRoot(aT道,i), getRoot(aT鉄道,i))
        aRet.append(Comb)
        if Comb in dRet:
            dRet[Comb] += 1
        else:
            dRet[Comb] = 1
    print(" ".join([str(dRet[_]) for _ in aRet]))

解()