Buggy python submission (verdict: Time Limit Exceeded):
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[i, 1] for i in range(N+1)]
	rail_graph = [[i, 1] for i in range(N+1)]

	def root(graph, i):
		parent = graph[i][0]
		if parent == i:
			return i
		return root(graph, parent)

	def is_same_tree(graph, i, j):
		return root(graph, i) == root(graph, j)

	def unite(graph, i, j):
		if is_same_tree(graph, i, j):
			return
		l = graph[root(graph, i)]
		r = graph[root(graph, j)]
		if l[1] < r[1]:
			l[0] = r[0]
			r[1] += l[1]
		else:
			r[0] = l[0]
			l[1] += r[0]

	ans_list = [""]*N

	for i in range(K):
		road[i] = list(map(int, input().split()))
		unite(road_graph, road[i][0], road[i][1])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		unite(rail_graph, rail[i][0], rail[i][1])

	for i in range(1, N+1):
		both_connected = 1
		for j in range(1, N+1):
			if i == j:
				continue
			if is_same_tree(road_graph, i, j) and is_same_tree(rail_graph, i, j):
				both_connected += 1
		this_ans = str(both_connected)
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()

Fixed submission:
from collections import defaultdict
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[i, 0] for i in range(N+1)]
	rail_graph = [[i, 0] for i in range(N+1)]

	def root(graph, i):
		parent = graph[i][0]
		if parent == i:
			return i
		return root(graph, parent)

	def is_same_tree(graph, i, j):
		return root(graph, i) == root(graph, j)

	def unite(graph, i, j):
		if is_same_tree(graph, i, j):
			return
		l = graph[root(graph, i)]
		r = graph[root(graph, j)]
		if l[1] < r[1]:
			l[0] = r[0]
			r[1] += 1
		else:
			r[0] = l[0]
			l[1] += 1

	ans_list = [""]*N

	for i in range(K):
		road[i] = list(map(int, input().split()))
		unite(road_graph, road[i][0], road[i][1])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		unite(rail_graph, rail[i][0], rail[i][1])

	ans_memo = defaultdict(int)
	road_memo = [0]*(N+1)
	rail_memo = [0]*(N+1)
	for i in range(1, N+1):
		road_root = root(road_graph, i)
		rail_root = root(rail_graph, i)
		road_memo[i] = road_root
		rail_memo[i] = rail_root
		ans_memo[(road_root, rail_root)] += 1

	for i in range(1, N+1):
		road_root = road_memo[i]
		rail_root = rail_memo[i]
		this_ans = str(ans_memo[road_root, rail_root])
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[] for _ in range(N+1)]
	rail_graph = [[] for _ in range(N+1)]
	ans_list = [""]*N
	for i in range(K):
		road[i] = list(map(int, input().split()))
		road_graph[road[i][0]].append(road[i][1])
		road_graph[road[i][1]].append(road[i][0])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		rail_graph[rail[i][0]].append(rail[i][1])
		rail_graph[rail[i][1]].append(rail[i][0])

	def dfs(start, graph):
		this_ans = set()
		stack = []
		stack.extend(graph[start])
		this_ans.add(start)
		while stack:
			cur = stack.pop()
			if cur in this_ans:
				continue
			this_ans.add(cur)
			stack.extend(graph[cur])
		return this_ans

	road_appeared = defaultdict(set)
	rail_appeared = defaultdict(set)
	for i in range(1, N+1):
		if i not in road_appeared:
			this_roads = dfs(i, road_graph)
			for this_road in this_roads:
				road_appeared[this_road] = this_roads

		if i not in rail_appeared:
			this_rails = dfs(i, rail_graph)
			for this_rail in this_rails:
				rail_appeared[this_rail] = this_rails

		both_connected = road_appeared[i] & rail_appeared[i]
		this_ans = str(len(both_connected))
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()

Fixed submission:
from collections import defaultdict
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[i, 0] for i in range(N+1)]
	rail_graph = [[i, 0] for i in range(N+1)]

	def root(graph, i):
		parent = graph[i][0]
		if parent == i:
			return i
		return root(graph, parent)

	def is_same_tree(graph, i, j):
		return root(graph, i) == root(graph, j)

	def unite(graph, i, j):
		if is_same_tree(graph, i, j):
			return
		l = graph[root(graph, i)]
		r = graph[root(graph, j)]
		if l[1] < r[1]:
			l[0] = r[0]
			r[1] += 1
		else:
			r[0] = l[0]
			l[1] += 1

	ans_list = [""]*N

	for i in range(K):
		road[i] = list(map(int, input().split()))
		unite(road_graph, road[i][0], road[i][1])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		unite(rail_graph, rail[i][0], rail[i][1])

	ans_memo = defaultdict(int)
	road_memo = [0]*(N+1)
	rail_memo = [0]*(N+1)
	for i in range(1, N+1):
		road_root = root(road_graph, i)
		rail_root = root(rail_graph, i)
		road_memo[i] = road_root
		rail_memo[i] = rail_root
		ans_memo[(road_root, rail_root)] += 1

	for i in range(1, N+1):
		road_root = road_memo[i]
		rail_root = rail_memo[i]
		this_ans = str(ans_memo[road_root, rail_root])
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[] for _ in range(N+1)]
	rail_graph = [[] for _ in range(N+1)]
	ans_list = [""]*N
	for i in range(K):
		road[i] = list(map(int, input().split()))
		road_graph[road[i][0]].append(road[i][1])
		road_graph[road[i][1]].append(road[i][0])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		rail_graph[rail[i][0]].append(rail[i][1])
		rail_graph[rail[i][1]].append(rail[i][0])

	def dfs(start, graph):
		this_ans = set()
		stack = []
		stack.extend(graph[start])
		this_ans.add(start)
		while stack:
			cur = stack.pop()
			if cur in this_ans:
				continue
			this_ans.add(cur)
			stack.extend(graph[cur])
		return this_ans

	appeared = set()
	for i in range(1, N+1):
		if i in appeared:
			continue
		both_connected = dfs(i, road_graph) & dfs(i, rail_graph)
		this_ans = str(len(both_connected))
		for city in both_connected:
			ans_list[city-1] = this_ans
			appeared.add(city)

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()

Fixed submission:
from collections import defaultdict
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[i, 0] for i in range(N+1)]
	rail_graph = [[i, 0] for i in range(N+1)]

	def root(graph, i):
		parent = graph[i][0]
		if parent == i:
			return i
		return root(graph, parent)

	def is_same_tree(graph, i, j):
		return root(graph, i) == root(graph, j)

	def unite(graph, i, j):
		if is_same_tree(graph, i, j):
			return
		l = graph[root(graph, i)]
		r = graph[root(graph, j)]
		if l[1] < r[1]:
			l[0] = r[0]
			r[1] += 1
		else:
			r[0] = l[0]
			l[1] += 1

	ans_list = [""]*N

	for i in range(K):
		road[i] = list(map(int, input().split()))
		unite(road_graph, road[i][0], road[i][1])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		unite(rail_graph, rail[i][0], rail[i][1])

	ans_memo = defaultdict(int)
	road_memo = [0]*(N+1)
	rail_memo = [0]*(N+1)
	for i in range(1, N+1):
		road_root = root(road_graph, i)
		rail_root = root(rail_graph, i)
		road_memo[i] = road_root
		rail_memo[i] = rail_root
		ans_memo[(road_root, rail_root)] += 1

	for i in range(1, N+1):
		road_root = road_memo[i]
		rail_root = rail_memo[i]
		this_ans = str(ans_memo[road_root, rail_root])
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()