Problem p03855 (python)

Fixed submission:
import sys
input = sys.stdin.buffer.readline
from collections import defaultdict
def main():
    def find(x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par[x])
            return par[x]
    def unite(x, y):
        x = find(x)
        y = find(y)
        if x == y:
            return False
        else:
            if par[x] > par[y]:
                x, y = y, x
            par[x] += par[y]
            par[y] = x
            return True
    def members(x):
        root = find(x)
        return [i for i in range(n) if find(i) == root]

    def find2(x):
        if par2[x] < 0:
            return x
        else:
            par2[x] = find2(par2[x])
            return par2[x]
    def unite2(x, y):
        x = find2(x)
        y = find2(y)
        if x == y:
            return False
        else:
            if par2[x] > par2[y]:
                x, y = y, x
            par2[x] += par2[y]
            par2[y] = x
            return True
    def members2(x):
        root2 = find2(x)
        return [i for i in range(n) if find2(i) == root2]

    n, k, l = map(int, input().split())
    par = [-1] * n
    par2 = [-1] * n
    for _ in range(k):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        unite(a, b)
    for _ in range(l):
        c, d = map(int, input().split())
        c -= 1
        d -= 1
        unite2(c, d)

    root_pairs = defaultdict(int)
    for i in range(n):
        root_pairs[find(i), find2(i)] += 1

    for i in range(n):
        print(root_pairs[find(i), find2(i)], end=' ')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

road = {}
train = {}
node_seen = set()
road_belong_group = {}
train_belong_group = {}
road_group = {}
train_group = {}

def dfs_road(t_node, group_num):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num
    road_group[group_num].add(t_node)
    node_seen.add(t_node)
    next_nodes = set(road[t_node]) - node_seen
    if len(next_nodes) == 0:
        return
    else:
        for nextnr in next_nodes:
            dfs_road(nextnr, group_num)

def dfs_train(t_node, group_num):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num
    train_group[group_num].add(t_node)
    node_seen.add(t_node)
    next_nodes = set(train[t_node]) - node_seen
    if len(next_nodes) == 0:
        return
    else:
        for nextnt in next_nodes:
            dfs_train(nextnt, group_num)

def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group
    # 入力
    n, k, l = map(int, input().split())
    #node_all = set([i for i in range(1, n + 1)])
    road = {e: [] for e in range(1, n + 1)}
    train = {e: [] for e in range(1, n + 1)}
    road_belong_group = {e: -1 for e in range(1, n + 1)}
    train_belong_group = {e: -1 for e in range(1, n + 1)}

    for _ in range(k):
        p, q = map(int, input().split())
        road[p].append(q)
        road[q].append(p)

    for _ in range(l):
        s, t = map(int, input().split())
        train[s].append(t)
        train[t].append(s)

    # 処理
    ## 道路

    group_num_road = 1
    for i_road in range(1, n + 1):
        if road_belong_group[i_road] != -1:
            continue
        if not road[i_road]:
            continue
        road_group[group_num_road] = set()
        dfs_road(i_road, group_num_road)
        group_num_road += 1

    ## 鉄道
    node_seen = set([])
    group_num_train = 1
    for i_train in range(1, n + 1):
        if train_belong_group[i_train] != -1:
            continue
        if not train[i_train]:
            continue
        train_group[group_num_train] = set()
        dfs_train(i_train, group_num_train)
        group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = road_belong_group[i]
        tg = train_belong_group[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_group[rg].intersection(train_group[tg]))
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

road = {}
train = {}
node_all = set()
node_seen = set()
road_belong_group = []
train_belong_group = []
road_group = {}
train_group = {}

def dfs_road(t_node, group_num):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num
    road_group[group_num].add(t_node)
    node_seen.add(t_node)
    next_nodes = set(road[t_node]) - node_seen
    if len(next_nodes) == 0:
        return
    else:
        for nextn in next_nodes:
            dfs_road(nextn, group_num)

def dfs_train(t_node, group_num):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num
    train_group[group_num].add(t_node)
    node_seen.add(t_node)
    next_nodes = set(train[t_node]) - node_seen
    if len(next_nodes) == 0:
        return
    else:
        for nextn in next_nodes:
            dfs_train(nextn, group_num)

def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group
    # 入力
    n, k, l = map(int, input().split())
    #node_all = set([i for i in range(1, n + 1)])
    road = {e: [] for e in range(1, n + 1)}
    train = {e: [] for e in range(1, n + 1)}
    road_belong_group = {e: -1 for e in range(1, n + 1)}
    train_belong_group = {e: -1 for e in range(1, n + 1)}

    for _ in range(k):
        p, q = map(int, input().split())
        road[p].append(q)
        road[q].append(p)

    for _ in range(l):
        s, t = map(int, input().split())
        train[s].append(t)
        train[t].append(s)

    # 処理
    ## 道路

    group_num_road = 1
    for i_road in range(1, n + 1):
        if road_belong_group[i_road] != -1:
            continue
        if not road[i_road]:
            continue
        road_group[group_num_road] = set()
        dfs_road(i_road, group_num_road)
        group_num_road += 1

    ## 鉄道
    node_seen = set([])
    group_num_train = 1
    for i_train in range(1, n + 1):
        if train_belong_group[i_train] != -1:
            continue
        if not train[i_train]:
            continue
        train_group[group_num_train] = set()
        dfs_train(i_train, group_num_train)
        group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = road_belong_group[i]
        tg = train_belong_group[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_group[rg] & train_group[tg] )
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

import sys
input = sys.stdin.readline

road = {}
train = {}
node_all = set()
node_seen = set()
road_belong_group = []
train_belong_group = []
road_group = {}
train_group = {}

def dfs_road(t_node, group_num):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num
    road_group[group_num].add(t_node)
    node_seen.add(t_node)
    #next_nodes = set(road[t_node]) - node_seen
    next_nodes = road[t_node]
    if len(next_nodes) == 0:
        return
    else:
        for next in next_nodes:
            if next not in node_seen:
                dfs_road(next, group_num)

def dfs_train(t_node, group_num):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num
    train_group[group_num].add(t_node)
    node_seen.add(t_node)
    #next_nodes = set(train[t_node]) - node_seen
    next_nodes = train[t_node]
    if len(next_nodes) == 0:
        return
    else:
        for next in next_nodes:
            if next not in node_seen:
                dfs_train(next, group_num)

def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group
    # 入力
    n, k, l = map(int, input().split())
    node_all = set([i for i in range(1, n + 1)])
    road = {e:[] for e in range(1, n + 1)}
    train = {e: [] for e in range(1, n + 1)}
    road_belong_group = {e: -1 for e in range(1, n + 1)}
    train_belong_group = {e: -1 for e in range(1, n + 1)}
    node_checked_road = 0
    node_checked_train = 0

    for _ in range(k):
        p, q = map(int, input().split())
        road[p].append(q)
        road[q].append(p)

    for _ in range(l):
        s, t = map(int, input().split())
        train[s].append(t)
        train[t].append(s)

    # 処理
    ## 道路
    road_group[-1] = set()
    train_group[-1] = set()

    group_num_road = 1
    for i_road in range(1, n + 1):
        if road_belong_group[i_road] != -1:
            continue
        if not road[i_road]:
            continue
        road_group[group_num_road] = set()
        dfs_road(i_road, group_num_road)
        group_num_road += 1

    ## 鉄道
    node_seen = set()
    group_num_train = 1
    for i_train in range(1, n + 1):
        if train_belong_group[i_train] != -1:
            continue
        if not train[i_train]:
            continue
        train_group[group_num_train] = set()
        dfs_train(i_train, group_num_train)
        group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = road_belong_group[i]
        tg = train_belong_group[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_group[rg] & train_group[tg] )
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

road = {}
train = {}
node_all = set()
node_seen = set()
road_belong_group = []
train_belong_group = []
road_group = {}
train_group = {}

def dfs_road(t_node, group_num):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num
    road_group[group_num].add(t_node)
    node_seen.add(t_node)
    next_nodes = set(road[t_node]) - node_seen
    if len(next_nodes) == 0:
        return
    else:
        for next in next_nodes:
            dfs_road(next, group_num)

def dfs_train(t_node, group_num):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num
    train_group[group_num].add(t_node)
    node_seen.add(t_node)
    next_nodes = set(train[t_node]) - node_seen
    if len(next_nodes) == 0:
        return
    else:
        for next in next_nodes:
            dfs_train(next, group_num)

def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group
    # 入力
    n, k, l = map(int, input().split())
    node_all = set([i for i in range(1, n + 1)])
    road = {e:[] for e in range(1, n + 1)}
    train = {e: [] for e in range(1, n + 1)}
    road_belong_group = {e: -1 for e in range(1, n + 1)}
    train_belong_group = {e: -1 for e in range(1, n + 1)}
    node_checked_road = 0
    node_checked_train = 0

    for _ in range(k):
        p, q = map(int, input().split())
        road[p].append(q)
        road[q].append(p)

    for _ in range(l):
        s, t = map(int, input().split())
        train[s].append(t)
        train[t].append(s)

    # 処理
    ## 道路
    road_group[-1] = set()
    train_group[-1] = set()

    group_num_road = 1
    for i_road in range(1, n + 1):
        if road_belong_group[i_road] != -1:
            continue
        if not road[i_road]:
            continue
        road_group[group_num_road] = set()
        dfs_road(i_road, group_num_road)
        group_num_road += 1

    ## 鉄道
    node_seen = set()
    group_num_train = 1
    for i_train in range(1, n + 1):
        if train_belong_group[i_train] != -1:
            continue
        if not train[i_train]:
            continue
        train_group[group_num_train] = set()
        dfs_train(i_train, group_num_train)
        group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = road_belong_group[i]
        tg = train_belong_group[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_group[rg] & train_group[tg] )
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

import sys
input = sys.stdin.readline
large_p = 10**9 + 7

road = {}
train = {}
node_all = set()
node_seen = set()
road_belong_group = []
train_belong_group = []
road_group = {}
train_group = {}

def dfs_road(t_node, group_num):
    global node_seen, road_group, road_belong_group
    node_seen.add(t_node)
    road_belong_group[t_node] = group_num
    road_group[group_num].add(t_node)
    next_nodes = set(road[t_node]) - node_seen
    if not list(next_nodes):
        return
    else:
        for next in next_nodes:
            dfs_road(next, group_num)

def dfs_train(t_node, group_num):
    global node_seen, train_group, train_belong_group
    node_seen.add(t_node)
    train_belong_group[t_node] = group_num
    train_group[group_num].add(t_node)
    next_nodes = set(train[t_node]) - node_seen
    if not list(next_nodes):
        return
    else:
        for next in next_nodes:
            dfs_train(next, group_num)

def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group
    # 入力
    n, k, l = map(int, input().split())
    node_all = set([i for i in range(1, n + 1)])
    road = {e:[] for e in range(1, n + 1)}
    road_belong_group = [0] * (n + 1)
    train_belong_group = [0] * (n + 1)
    road_belong_group[0] = -1
    train_belong_group[0] = -1

    for _ in range(k):
        p, q = map(int, input().split())
        road[p].append(q)
        road[q].append(p)
    train = {e:[] for e in range(1, n + 1)}
    for _ in range(l):
        s, t = map(int, input().split())
        train[s].append(t)
        train[t].append(s)

    # 処理
    ## 道路
    road_group[-1] = set()
    train_group[-1] = set()

    group_num_road = 1
    while list(node_all - node_seen):
        node_remain = road_belong_group.index(0)
        if node_remain == 0:
            node_remain = 1
        if not road[node_remain]:
            road_belong_group[node_remain] = -1
            node_seen.add(node_remain)
            continue
        road_group[group_num_road] = set()
        dfs_road(node_remain, group_num_road)
        group_num_road += 1


    ## 鉄道
    group_num_train = 1
    node_seen = set()
    while list(node_all - node_seen):
        node_remain = train_belong_group.index(0)
        if node_remain == 0:
            node_remain = 1
        if not train[node_remain]:
            train_belong_group[node_remain] = -1
            node_seen.add(node_remain)
            continue
        train_group[group_num_train] = set()
        dfs_train(node_remain, group_num_train)
        group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        linked_city = len(road_group[road_belong_group[i]] & train_group[train_belong_group[i]] )
        if linked_city == 0:
            linked_city += 1
        print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

import sys
input = sys.stdin.readline

road = {}
train = {}
node_all = set()
node_seen = set()
road_belong_group = []
train_belong_group = []
road_group = {}
train_group = {}

def dfs_road(t_node, group_num):
    global node_seen, road_group, road_belong_group
    node_seen.add(t_node)
    road_belong_group[t_node] = group_num
    road_group[group_num].add(t_node)
    next_nodes = set(road[t_node]) - node_seen
    if not list(next_nodes):
        return
    else:
        for next in next_nodes:
            dfs_road(next, group_num)

def dfs_train(t_node, group_num):
    global node_seen, train_group, train_belong_group
    node_seen.add(t_node)
    train_belong_group[t_node] = group_num
    train_group[group_num].add(t_node)
    next_nodes = set(train[t_node]) - node_seen
    if not list(next_nodes):
        return
    else:
        for next in next_nodes:
            dfs_train(next, group_num)

def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group
    # 入力
    n, k, l = map(int, input().split())
    node_all = set([i for i in range(1, n + 1)])
    road = {e:[] for e in range(1, n + 1)}
    road_belong_group = [0] * (n + 1)
    train_belong_group = [0] * (n + 1)
    road_belong_group[0] = -1
    train_belong_group[0] = -1

    for _ in range(k):
        p, q = map(int, input().split())
        road[p].append(q)
        road[q].append(p)
    train = {e:[] for e in range(1, n + 1)}
    for _ in range(l):
        s, t = map(int, input().split())
        train[s].append(t)
        train[t].append(s)

    # 処理
    ## 道路
    road_group[-1] = set()
    train_group[-1] = set()

    group_num_road = 1
    while list(node_all - node_seen):
        node_remain = road_belong_group.index(0)
        if node_remain == 0:
            node_remain = 1
        if not road[node_remain]:
            road_belong_group[node_remain] = -1
            node_seen.add(node_remain)
            continue
        road_group[group_num_road] = set()
        dfs_road(node_remain, group_num_road)
        group_num_road += 1


    ## 鉄道
    group_num_train = 1
    node_seen = set()
    while list(node_all - node_seen):
        node_remain = train_belong_group.index(0)
        if node_remain == 0:
            node_remain = 1
        if not train[node_remain]:
            train_belong_group[node_remain] = -1
            node_seen.add(node_remain)
            continue
        train_group[group_num_train] = set()
        dfs_train(node_remain, group_num_train)
        group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        linked_city = len(road_group[road_belong_group[i]] & train_group[train_belong_group[i]] )
        if linked_city == 0:
            linked_city += 1
        print(linked_city, end = ' ')


if __name__ == '__main__':
    main()