Buggy python submission (verdict: Time Limit Exceeded):
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)
    for i in range(n):
        road = set(members(i))
        train = set(members2(i))
        #print(road, train)
        print(len(road & train), end=' ')

if __name__ == '__main__':
    main()


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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    # 入力
    n, k, l = map(int, input().split())
    road_a = [None] * k
    road_b = [None] * k
    road = [[] for _ in range(n + 1)]
    train_a = [None] * l
    train_b = [None] * l
    train = [[] for _ in range(n + 1)]
    city_group_in_road = {e: -1 for e in range(1, n + 1)}
    city_group_in_train = {e: -1 for e in range(1, n + 1)}
    road_groups = {}
    train_groups = {}
    stack_road = deque()
    stack_train = deque()

    for i1 in range(k):
        road_a[i1], road_b[i1] = map(int, input().split())
        road[road_a[i1]].append(road_b[i1])
        road[road_b[i1]].append(road_a[i1])
    for i2 in range(l):
        train_a[i2], train_b[i2] = map(int, input().split())
        train[train_a[i2]].append(train_b[i2])
        train[train_b[i2]].append(train_a[i2])

    # 処理
    ## 道路
    group_num_road = 1
    for i_road in range(1, n + 1):
        if city_group_in_road[i_road] == -1 and road[i_road] != []:
            road_groups[group_num_road] = set()
            road_groups[group_num_road].add(i_road)
            city_group_in_road[i_road] = group_num_road
            stack_road += road[i_road]
            while stack_road:
                next = stack_road.popleft()
                if city_group_in_road[next] != -1:
                    continue
                road_groups[group_num_road].add(next)
                city_group_in_road[next] = group_num_road
                stack_road += road[next]
            group_num_road += 1


    ## 鉄道
    group_num_train = 1
    for i_train in range(1, n + 1):
        if city_group_in_train[i_train] == -1 and train[i_train] != [] and city_group_in_road[i_train] != -1:
            train_groups[group_num_train] = set()
            train_groups[group_num_train].add(i_train)
            city_group_in_train[i_train] = group_num_train
            stack_train += train[i_train]
            while stack_train:
                next = stack_train.popleft()
                if city_group_in_train[next] != -1:
                    continue
                train_groups[group_num_train].add(next)
                city_group_in_train[next] = group_num_train
                stack_train += train[next]
            group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = city_group_in_road[i]
        tg = city_group_in_train[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_groups[rg].intersection(train_groups[tg]))
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    # 入力
    n, k, l = map(int, input().split())
    road_a = [None] * k
    road_b = [None] * k
    road = [[] for _ in range(n + 1)]
    train_a = [None] * l
    train_b = [None] * l
    train = [[] for _ in range(n + 1)]
    city_group_in_road = {e: -1 for e in range(1, n + 1)}
    city_group_in_train = {e: -1 for e in range(1, n + 1)}
    road_groups = {}
    train_groups = {}
    stack_road = deque()
    stack_train = deque()

    for i1 in range(k):
        road_a[i1], road_b[i1] = map(int, input().split())
        road[road_a[i1]].append(road_b[i1])
        road[road_b[i1]].append(road_a[i1])
    for i2 in range(l):
        train_a[i2], train_b[i2] = map(int, input().split())
        train[train_a[i2]].append(train_b[i2])
        train[train_b[i2]].append(train_a[i2])

    # 処理
    ## 道路
    group_num_road = 1
    for i_road in range(1, n + 1):
        if city_group_in_road[i_road] == -1 and road[i_road] != []:
            road_groups[group_num_road] = set()
            road_groups[group_num_road].add(i_road)
            city_group_in_road[i_road] = group_num_road
            stack_road += road[i_road]
            while stack_road:
                next = stack_road.popleft()
                if city_group_in_road[next] != -1:
                    continue
                road_groups[group_num_road].add(next)
                city_group_in_road[next] = group_num_road
                stack_road += road[next]
            group_num_road += 1


    ## 鉄道
    group_num_train = 1
    for i_train in range(1, n + 1):
        if city_group_in_train[i_train] == -1 and train[i_train] != []:
            train_groups[group_num_train] = set()
            train_groups[group_num_train].add(i_train)
            city_group_in_train[i_train] = group_num_train
            stack_train += train[i_train]
            while stack_train:
                next = stack_train.popleft()
                if city_group_in_train[next] != -1:
                    continue
                train_groups[group_num_train].add(next)
                city_group_in_train[next] = group_num_train
                stack_train += train[next]
            group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = city_group_in_road[i]
        tg = city_group_in_train[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_groups[rg].intersection(train_groups[tg]))
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()

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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    # 入力
    n, k, l = map(int, input().split())
    road_a = [None] * k
    road_b = [None] * k
    road = [[] for _ in range(n + 1)]
    train_a = [None] * l
    train_b = [None] * l
    train = [[] for _ in range(n + 1)]
    city_group_in_road = {e: -1 for e in range(1, n + 1)}
    city_group_in_train = {e: -1 for e in range(1, n + 1)}
    road_groups = {}
    train_groups = {}
    stack_road = deque()
    stack_train = deque()

    for i1 in range(k):
        road_a[i1], road_b[i1] = map(int, input().split())
        road[road_a[i1]].append(road_b[i1])
        road[road_b[i1]].append(road_a[i1])
    for i2 in range(l):
        train_a[i2], train_b[i2] = map(int, input().split())
        train[train_a[i2]].append(train_b[i2])
        train[train_b[i2]].append(train_a[i2])

    # 処理
    ## 道路
    group_num_road = 1
    for i_road in range(1, n + 1):
        if city_group_in_road[i_road] == -1:
            road_groups[group_num_road] = set()
            road_groups[group_num_road].add(i_road)
            city_group_in_road[i_road] = group_num_road
            stack_road += road[i_road]
            while stack_road:
                next = stack_road.popleft()
                if city_group_in_road[next] != -1:
                    continue
                road_groups[group_num_road].add(next)
                city_group_in_road[next] = group_num_road
                stack_road += road[next]
            group_num_road += 1


    ## 鉄道
    group_num_train = 1
    for i_train in range(1, n + 1):
        if city_group_in_train[i_train] == -1:
            train_groups[group_num_train] = set()
            train_groups[group_num_train].add(i_train)
            city_group_in_train[i_train] = group_num_train
            stack_train += train[i_train]
            while stack_train:
                next = stack_train.popleft()
                if city_group_in_train[next] != -1:
                    continue
                train_groups[group_num_train].add(next)
                city_group_in_train[next] = group_num_train
                stack_train += train[next]
            group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = city_group_in_road[i]
        tg = city_group_in_train[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_groups[rg].intersection(train_groups[tg]))
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()


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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    # 入力
    n, k, l = map(int, input().split())
    road = {e: [] for e in range(1, n + 1)}
    train = {e: [] for e in range(1, n + 1)}
    city_group_in_road = {e: -1 for e in range(1, n + 1)}
    city_group_in_train = {e: -1 for e in range(1, n + 1)}
    road_groups = {}
    train_groups = {}
    stack_road = deque()
    stack_train = deque()

    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 city_group_in_road[i_road] == -1:
            road_groups[group_num_road] = set()
            road_groups[group_num_road].add(i_road)
            city_group_in_road[i_road] = group_num_road
            stack_road += road[i_road]
            while stack_road:
                next = stack_road.popleft()
                if city_group_in_road[next] != -1:
                    continue
                road_groups[group_num_road].add(next)
                city_group_in_road[next] = group_num_road
                stack_road += road[next]
            group_num_road += 1
    

    ## 鉄道
    group_num_train = 1
    for i_train in range(1, n + 1):
        if city_group_in_train[i_train] == -1:
            train_groups[group_num_train] = set()
            train_groups[group_num_train].add(i_train)
            city_group_in_train[i_train] = group_num_train
            stack_train += train[i_train]
            while stack_train:
                next = stack_train.popleft()
                if city_group_in_train[next] != -1:
                    continue
                train_groups[group_num_train].add(next)
                city_group_in_train[next] = group_num_train
                stack_train += train[next]
            group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = city_group_in_road[i]
        tg = city_group_in_train[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_groups[rg].intersection(train_groups[tg]))
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()


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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import deque

def main():
    # 入力
    n, k, l = map(int, input().split())
    road = {e: [] for e in range(1, n + 1)}
    train = {e: [] for e in range(1, n + 1)}
    city_group_in_road = {e: -1 for e in range(1, n + 1)}
    city_group_in_train = {e: -1 for e in range(1, n + 1)}
    road_groups = {}
    train_groups = {}
    stack_road = deque()
    stack_train = deque()

    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 city_group_in_road[i_road] == -1:
            road_groups[group_num_road] = set()
            road_groups[group_num_road].add(i_road)
            city_group_in_road[i_road] = group_num_road
            stack_road += road[i_road]
            while stack_road:
                next = stack_road.popleft()
                if city_group_in_road[next] != -1:
                    continue
                road_groups[group_num_road].add(next)
                city_group_in_road[next] = group_num_road
                stack_road += road[next]
            group_num_road += 1
    

    ## 鉄道
    group_num_train = 1
    for i_train in range(1, n + 1):
        if city_group_in_train[i_train] == -1:
            train_groups[group_num_train] = set()
            train_groups[group_num_train].add(i_train)
            city_group_in_train[i_train] = group_num_train
            stack_train += train[i_train]
            while stack_train:
                next = stack_train.popleft()
                if city_group_in_train[next] != -1:
                    continue
                train_groups[group_num_train].add(next)
                city_group_in_train[next] = group_num_train
                stack_train += train[next]
            group_num_train += 1

    # 出力
    for i in range(1, n + 1):
        rg = city_group_in_road[i]
        tg = city_group_in_train[i]
        if rg == -1 or tg == -1:
            print(1, end= ' ')
        else:
            linked_city = len(road_groups[rg].intersection(train_groups[tg]))
            if linked_city == 0:
                linked_city += 1
            print(linked_city, end = ' ')


if __name__ == '__main__':
    main()


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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)
from collections import deque
from copy import deepcopy

road = {}
train = {}
node_seen_road = set()
node_seen_train = set()
road_belong_group = {}
train_belong_group = {}
road_group = {}
train_group = {}
group_num_road = 0
group_num_train = 0
city_without_road = set()

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

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


def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group, group_num_road, group_num_train, city_without_road
    # 入力
    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)}
    #city_list_forroad = deque([j for j in range(1, n + 1)])
    #city_list_fortrain = deepcopy(city_list_forroad)


    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 i_road in node_seen_road:
            continue
        if not road[i_road]:
            continue
        road_group[group_num_road] = set()
        dfs_road(i_road)
        group_num_road += 1
    #city_without_road = set([ci for ci in road_belong_group if road_belong_group[ci] == -1])

    ## 鉄道

    group_num_train = 1
    for i_train in range(1, n + 1):
        if i_train in node_seen_train or i_train not in node_seen_road:
            continue
        if not train[i_train]:
            continue
        train_group[group_num_train] = set()
        dfs_train(i_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()

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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)

road = {}
train = {}
node_seen_road = set()
node_seen_train = set()
road_belong_group = {}
train_belong_group = {}
road_group = {}
train_group = {}
group_num_road = 0
group_num_train = 0
city_without_road = set()

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

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


def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group, group_num_road, group_num_train, city_without_road
    # 入力
    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 += 1
    #city_without_road = set([ci for ci in road_belong_group if road_belong_group[ci] == -1])

    ## 鉄道

    group_num_train = 1
    for i_train in range(1, n + 1):
        if train_belong_group[i_train] != -1 or i_train not in node_seen_road:
            continue
        if not train[i_train]:
            continue
        train_group[group_num_train] = set()
        dfs_train(i_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()

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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

road = {}
train = {}
node_seen = set()
road_belong_group = {}
train_belong_group = {}
road_group = {}
train_group = {}
group_num_road = 0
group_num_train = 0
city_without_road = set()

def dfs_road(t_node):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num_road
    road_group[group_num_road].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)

def dfs_train(t_node):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num_train
    train_group[group_num_train].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)


def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group, group_num_road, group_num_train, city_without_road
    # 入力
    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 += 1
    city_without_road = set([ci for ci in road_belong_group if road_belong_group[ci] == -1])

    ## 鉄道
    node_seen = set([])
    group_num_train = 1
    for i_train in range(1, n + 1):
        if train_belong_group[i_train] != -1 or road_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 += 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()

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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

def dfs_road(t_node):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num_road
    road_group[group_num_road].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)

def dfs_train(t_node):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num_train
    train_group[group_num_train].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)


def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group, group_num_road, group_num_train
    # 入力
    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 += 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 += 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()

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 python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

def dfs_road(t_node):
    global road_group, road_belong_group, node_seen
    road_belong_group[t_node] = group_num_road
    road_group[group_num_road].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)

def dfs_train(t_node):
    global node_seen, train_group, train_belong_group
    train_belong_group[t_node] = group_num_train
    train_group[group_num_train].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)


def main():
    global road, train, node_seen, road_group, train_group, node_all, road_belong_group,train_belong_group, group_num_road, group_num_train
    # 入力
    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 += 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 += 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()

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 python 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()


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 python 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()

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 python 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()


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 python 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()


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 python 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()


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 python 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()


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()