Problem p03855 (python)

Fixed submission:
from collections import defaultdict
def build_tree(N, edges):
    root_list = [i for i in range(N)]
    for p, q in edges:
        p -= 1
        q -= 1
        root_p = get_root(root_list, p)
        root_q = get_root(root_list, q)
        root_now = min(root_p, root_q, p)
        root_list[p] = root_now
        root_list[q] = root_now
        root_list[root_p] = root_now
        root_list[root_q] = root_now
    return root_list

def get_root(root_list, city):
    if root_list[city] == city:
        return city
    else: 
        root_list[city] = get_root(root_list, root_list[city])
        return root_list[city]

if __name__ == '__main__':
    N, K, L = map(int, input().split())
    roads = [None]*K
    rails = [None]*L
    for k in range(K):
        roads[k] = list(map(int, input().split()))
    for l in range(L):
        rails[l] = list(map(int, input().split()))
    
    
    '''N = 2*10**5
    L = int(N/2)
    K = int(N/2)
    roads = [None]*K
    rails = [None]*L
    for i in range(L):
        roads[i] = [2*i, 2*i+1]
    for i in range(K):
        rails[i] = [2*i, 2*i+1]'''
    
    road_roots = build_tree(N, roads)
    rail_roots = build_tree(N, rails)
    
    railroads = defaultdict(int)
    RR = []
    for n in range(N):
        rr = (get_root(road_roots, n), get_root(rail_roots, n))
        RR.append(rr)
        railroads[rr] += 1
    answer = [railroads[key] for key in RR]
    print(' '.join(map(str, answer)))

Buggy submission (verdict: Runtime Error):
def find_root(edges, root, group, before, now):
    group.append(now)
    children = edges[now]
    for child in children:
        if child != before and child not in group:
            root, group = find_root(edges, root, group, now, child)
    return root, group
    
if __name__ == '__main__':
    N, K, L = map(int, input().split())
    roads = [[] for _ in range(N)]
    rails = [[] for _ in range(N)]
    road_roots = [None]*N
    rail_roots = [None]*N
    answer = [1]*N
        
    for i in range(K):
        p, q = map(int, input().split())
        p -= 1
        q -= 1
        roads[p].append(q)
        roads[q].append(p)
    
    for i in range(L):
        r, s = map(int, input().split())
        r -= 1
        s -= 1
        rails[r].append(s)
        rails[s].append(r)
    
    for i, r in enumerate(road_roots):
        if r is None:
            root, group = find_root(roads, i, [], -1, i)
            for g in group:
                road_roots[g] = root

    for i, r in enumerate(rail_roots):
        if r is None:
            root, group = find_root(rails, i, [], -1, i)
            for g in group:
                rail_roots[g] = root
    railroads=[[rail, road] for rail, road in zip(road_roots, rail_roots)]

    for n in range(N):
        for d in range(1, N-n):
            if railroads[n] == railroads[n+d]:
                answer[n] += 1 
                answer[n+d] += 1
    print(' '.join(list(map(str, answer))))

Buggy submission (verdict: Runtime Error):
if __name__ == '__main__':
    N, K, L = map(int, input().split())
    PQ = [None]*K
    RS = [None]*L
    roads = [set() for _ in range(N)]
    rails = [set() for _ in range(N)]
    answer = [None]*N
        
    for i in range(K):
        PQ[i] = list(map(int, input().split()))
    #PQ = sorted(PQ, key=min)
    
    
    for i in range(K):
        if not roads[PQ[i][1] - 1]:
            roads[PQ[i][0] - 1].add(PQ[i][0] - 1)
            roads[PQ[i][0] - 1].add(PQ[i][1] - 1)
            roads[PQ[i][1] - 1] = roads[PQ[i][0] - 1]
        else:
            roads[PQ[i][1] - 1].add(PQ[i][0] - 1)
            roads[PQ[i][1] - 1].add(PQ[i][1] - 1)
            roads[PQ[i][0] - 1] = roads[PQ[i][1] - 1]

    for i in range(L):
        RS[i] = list(map(int, input().split()))
    #RS = sorted(RS, key=min)
    
    for i in range(L):
        if not rails[PQ[i][1] - 1]:
            rails[RS[i][0] - 1].add(RS[i][0] - 1)
            rails[RS[i][0] - 1].add(RS[i][1] - 1)
            rails[RS[i][1] - 1] = rails[RS[i][0] - 1]
        else:
            rails[RS[i][1] - 1].add(RS[i][0] - 1)
            rails[RS[i][1] - 1].add(RS[i][1] - 1)
            rails[RS[i][0] - 1] = rails[RS[i][1] - 1]

    for n in range(N):
        roads[n].add(n)
        rails[n].add(n)
        answer[n]= len(roads[n] & rails[n])
    
    
            
    print(' '.join(map(str, answer)))