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

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails

# nodes = [1, 2, ...]
# edges = [(x, y), (x, y), ...]
def graph_connected_component(nodes, nd_edges):

    # 無向グラフを想定、edgesを両向きにはやす
    edge_table = {n:[] for n in nodes}
    for e in nd_edges:
        edge_table[e[0]].append(e[1])
        edge_table[e[1]].append(e[0])

    reach_nodes = set()
    result = []
    while reach_nodes != set(nodes):
        node_q = deque()
        component = set()

        # まだ見ていないnodeを基点にしてcomponentを構成
        for node in nodes:
            if node not in reach_nodes:
                node_q.appendleft(node)
                break

        # qがある = 連結先があるので探索
        while len(node_q) > 0:
            curr = node_q.pop()
            reach_nodes.add(curr)
            component.add(curr)

            # 連結先のうち、まだ未探索nodeをqにつむ
            next_nodes = edge_table[curr]
            for node in next_nodes:
                if node not in component:
                    node_q.appendleft(node)

        result.append(component)
    return result


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))
    road_components = graph_connected_component(nodes, roads)
    rail_components = graph_connected_component(nodes, rails)

    road_ids = [-1] * (n + 1)
    rail_ids = [-1] * (n + 1)

    for i, c in enumerate(road_components):
        for node in c:
            road_ids[node] = i + 1

    for i, c in enumerate(rail_components):
        for node in c:
            rail_ids[node] = i + 1

    count = defaultdict(int)
    for node in nodes:
        count[(road_ids[node], rail_ids[node])] += 1

    print(' '.join([str(count[(road_ids[node], rail_ids[node])]) for node in nodes]))


if __name__ == '__main__':
    submit()


Fixed submission:
from collections import deque, defaultdict

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails


def set_ids(nodes, paths):
    edge_table = {n: [] for n in nodes}

    for p in paths:
        edge_table[p[0]].append(p[1])
        edge_table[p[1]].append(p[0])

    ids = [-1] * (len(nodes) + 1)
    q = deque()
    start = 1
    group = 1
    while True:
        # まだセットしていないnodeを見つける
        while ids[start] != -1:
            start += 1
            if start > len(nodes):
                return ids

        q.appendleft(start)
        while len(q) > 0:
            curr = q.popleft()
            ids[curr] = group

            for node in edge_table[curr]:
                if ids[node] == -1:
                    q.appendleft(node)
        group += 1


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))

    road_ids = set_ids(nodes, roads)
    rail_ids = set_ids(nodes, rails)

    count = defaultdict(int)
    for node in nodes:
        count[(road_ids[node], rail_ids[node])] += 1

    print(' '.join([str(count[(road_ids[node], rail_ids[node])]) for node in nodes]))


if __name__ == '__main__':
    submit()

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

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails

# nodes = [1, 2, ...]
# edges = [(x, y), (x, y), ...]
def graph_connected_component(nodes, nd_edges):

    # 無向グラフを想定、edgesを両向きにはやす
    edge_table = {n:[] for n in nodes}
    for e in nd_edges:
        edge_table[e[0]].append(e[1])
        edge_table[e[1]].append(e[0])

    reach_nodes = set()
    result = []
    while reach_nodes != set(nodes):
        node_q = deque()
        component = set()

        # まだ見ていないnodeを基点にしてcomponentを構成
        for node in nodes:
            if node not in reach_nodes:
                node_q.appendleft(node)
                break

        # qがある = 連結先があるので探索
        while len(node_q) > 0:
            curr = node_q.pop()
            reach_nodes.add(curr)
            component.add(curr)

            # 連結先のうち、まだ未探索nodeをqにつむ
            next_nodes = edge_table[curr]
            for node in next_nodes:
                if node not in component:
                    node_q.appendleft(node)

        result.append(component)
    return result


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))
    road_components = graph_connected_component(nodes, roads)
    rail_components = graph_connected_component(nodes, rails)

    road_comp_table = {}
    rail_comp_table = {}
    for road_comp in road_components:
        for node in road_comp:
            road_comp_table[node] = road_comp

    for rail_comp in rail_components:
        for node in rail_comp:
            road_comp_table[node] = road_comp_table[node].intersection(rail_comp)

    print(' '.join([str(len(road_comp_table[i])) for i in nodes]))



if __name__ == '__main__':
    submit()

Fixed submission:
from collections import deque, defaultdict

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails


def set_ids(nodes, paths):
    edge_table = {n: [] for n in nodes}

    for p in paths:
        edge_table[p[0]].append(p[1])
        edge_table[p[1]].append(p[0])

    ids = [-1] * (len(nodes) + 1)
    q = deque()
    start = 1
    group = 1
    while True:
        # まだセットしていないnodeを見つける
        while ids[start] != -1:
            start += 1
            if start > len(nodes):
                return ids

        q.appendleft(start)
        while len(q) > 0:
            curr = q.popleft()
            ids[curr] = group

            for node in edge_table[curr]:
                if ids[node] == -1:
                    q.appendleft(node)
        group += 1


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))

    road_ids = set_ids(nodes, roads)
    rail_ids = set_ids(nodes, rails)

    count = defaultdict(int)
    for node in nodes:
        count[(road_ids[node], rail_ids[node])] += 1

    print(' '.join([str(count[(road_ids[node], rail_ids[node])]) for node in nodes]))


if __name__ == '__main__':
    submit()

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

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails

# nodes = [1, 2, ...]
# edges = [(x, y), (x, y), ...]
def graph_connected_component(nodes, nd_edges):

    # 無向グラフを想定、edgesを両向きにはやす
    edge_table = {n:[] for n in nodes}
    for e in nd_edges:
        edge_table[e[0]].append(e[1])
        edge_table[e[1]].append(e[0])

    reach_nodes = set()
    result = []
    while reach_nodes != set(nodes):
        node_q = deque()
        component = set()

        # まだ見ていないnodeを基点にしてcomponentを構成
        for node in nodes:
            if node not in reach_nodes:
                node_q.appendleft(node)
                break

        # qがある = 連結先があるので探索
        while len(node_q) > 0:
            curr = node_q.pop()
            reach_nodes.add(curr)
            component.add(curr)

            # 連結先のうち、まだ未探索nodeをqにつむ
            next_nodes = edge_table[curr]
            for node in next_nodes:
                if node not in component:
                    node_q.appendleft(node)

        result.append(component)
    return result


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))
    road_components = graph_connected_component(nodes, roads)
    rail_components = graph_connected_component(nodes, rails)

    road_comp_table = {}
    rail_comp_table = {}
    for road_comp in road_components:
        for node in road_comp:
            road_comp_table[node] = road_comp

    for rail_comp in rail_components:
        for node in rail_comp:
            road_comp_table[node] = road_comp_table[node].intersection(rail_comp)

    print(' '.join([str(len(road_comp_table[i])) for i in nodes]))



if __name__ == '__main__':
    submit()

Fixed submission:
from collections import deque, defaultdict

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails


def set_ids(nodes, paths):
    edge_table = {n: [] for n in nodes}

    for p in paths:
        edge_table[p[0]].append(p[1])
        edge_table[p[1]].append(p[0])

    ids = [-1] * (len(nodes) + 1)
    q = deque()
    start = 1
    group = 1
    while True:
        # まだセットしていないnodeを見つける
        while ids[start] != -1:
            start += 1
            if start > len(nodes):
                return ids

        q.appendleft(start)
        while len(q) > 0:
            curr = q.popleft()
            ids[curr] = group

            for node in edge_table[curr]:
                if ids[node] == -1:
                    q.appendleft(node)
        group += 1


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))

    road_ids = set_ids(nodes, roads)
    rail_ids = set_ids(nodes, rails)

    count = defaultdict(int)
    for node in nodes:
        count[(road_ids[node], rail_ids[node])] += 1

    print(' '.join([str(count[(road_ids[node], rail_ids[node])]) for node in nodes]))


if __name__ == '__main__':
    submit()

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

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails

# nodes = [1, 2, ...]
# edges = [(x, y), (x, y), ...]
def graph_connected_component(nodes, nd_edges):

    # 無向グラフを想定、edgesを両向きにはやす
    edge_table = {n:[] for n in nodes}
    for e in nd_edges:
        edge_table[e[0]].append(e[1])
        edge_table[e[1]].append(e[0])

    reach_nodes = set()
    result = []
    while reach_nodes != set(nodes):
        node_q = deque()
        component = set()

        # まだ見ていないnodeを基点にしてcomponentを構成
        for node in nodes:
            if node not in reach_nodes:
                node_q.appendleft(node)
                break

        # qがある = 連結先があるので探索
        while len(node_q) > 0:
            curr = node_q.pop()
            reach_nodes.add(curr)
            component.add(curr)

            # 連結先のうち、まだ未探索nodeをqにつむ
            next_nodes = edge_table[curr]
            for node in next_nodes:
                if node not in component:
                    node_q.appendleft(node)

        result.append(component)
    return result


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))
    road_components = graph_connected_component(nodes, roads)
    rail_components = graph_connected_component(nodes, rails)

    road_comp_table = {}
    rail_comp_table = {}
    for road_comp in road_components:
        for node in road_comp:
            road_comp_table[node] = road_comp

    for rail_comp in rail_components:
        for node in rail_comp:
            rail_comp_table[node] = rail_comp

    count = [len(road_comp_table[n].intersection(rail_comp_table[n])) for n in nodes]
    print(' '.join(map(str, count)))


if __name__ == '__main__':
    submit()


Fixed submission:
from collections import deque, defaultdict

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails


def set_ids(nodes, paths):
    edge_table = {n: [] for n in nodes}

    for p in paths:
        edge_table[p[0]].append(p[1])
        edge_table[p[1]].append(p[0])

    ids = [-1] * (len(nodes) + 1)
    q = deque()
    start = 1
    group = 1
    while True:
        # まだセットしていないnodeを見つける
        while ids[start] != -1:
            start += 1
            if start > len(nodes):
                return ids

        q.appendleft(start)
        while len(q) > 0:
            curr = q.popleft()
            ids[curr] = group

            for node in edge_table[curr]:
                if ids[node] == -1:
                    q.appendleft(node)
        group += 1


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))

    road_ids = set_ids(nodes, roads)
    rail_ids = set_ids(nodes, rails)

    count = defaultdict(int)
    for node in nodes:
        count[(road_ids[node], rail_ids[node])] += 1

    print(' '.join([str(count[(road_ids[node], rail_ids[node])]) for node in nodes]))


if __name__ == '__main__':
    submit()

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

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails

# nodes = [1, 2, ...]
# edges = [(x, y), (x, y), ...]
def graph_connected_component(nodes, nd_edges):

    # 無向グラフを想定、edgesを両向きにはやす
    edge_table = {n:[] for n in nodes}
    for e in nd_edges:
        edge_table[e[0]].append(e[1])
        edge_table[e[1]].append(e[0])

    reach_nodes = set()
    result = []
    while reach_nodes != set(nodes):
        node_q = deque()
        component = set()

        # まだ見ていないnodeを基点にしてcomponentを構成
        for node in nodes:
            if node not in reach_nodes:
                node_q.appendleft(node)
                break

        # qがある = 連結先があるので探索
        while len(node_q) > 0:
            curr = node_q.pop()
            reach_nodes.add(curr)
            component.add(curr)

            # 連結先のうち、まだ未探索nodeをqにつむ
            next_nodes = edge_table[curr]
            for node in next_nodes:
                if node not in component:
                    node_q.appendleft(node)

        result.append(component)
    return result


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))
    road_components = graph_connected_component(nodes, roads)
    rail_components = graph_connected_component(nodes, rails)

    road_comp_table = {}
    rail_comp_table = {}
    for road_comp in road_components:
        for node in road_comp:
            road_comp_table[node] = road_comp

    for rail_comp in rail_components:
        for node in rail_comp:
            rail_comp_table[node] = rail_comp

    count = [len(road_comp_table[n].intersection(rail_comp_table[n])) for n in nodes]
    print(' '.join(map(str, count)))


if __name__ == '__main__':
    submit()


Fixed submission:
from collections import deque, defaultdict

def read_input():
    n, k, l = map(int, input().split())
    roads = []
    for i in range(k):
        p, q = map(int, input().split())
        roads.append((p, q))

    rails = []
    for j in range(l):
        r, s = map(int, input().split())
        rails.append((r, s))

    return n, k, l, roads, rails


def set_ids(nodes, paths):
    edge_table = {n: [] for n in nodes}

    for p in paths:
        edge_table[p[0]].append(p[1])
        edge_table[p[1]].append(p[0])

    ids = [-1] * (len(nodes) + 1)
    q = deque()
    start = 1
    group = 1
    while True:
        # まだセットしていないnodeを見つける
        while ids[start] != -1:
            start += 1
            if start > len(nodes):
                return ids

        q.appendleft(start)
        while len(q) > 0:
            curr = q.popleft()
            ids[curr] = group

            for node in edge_table[curr]:
                if ids[node] == -1:
                    q.appendleft(node)
        group += 1


def submit():
    n, k, l, roads, rails = read_input()
    nodes = list(range(1, n + 1))

    road_ids = set_ids(nodes, roads)
    rail_ids = set_ids(nodes, rails)

    count = defaultdict(int)
    for node in nodes:
        count[(road_ids[node], rail_ids[node])] += 1

    print(' '.join([str(count[(road_ids[node], rail_ids[node])]) for node in nodes]))


if __name__ == '__main__':
    submit()