Buggy python submission (verdict: Time Limit Exceeded):
N,K,L = map(int,input().split())
roads = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(K)]
rails = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(L)]

class UnionFind:
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.count = 0
    def root(self,a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]
    def is_same(self,a,b):
        return self.root(a) == self.root(b)
    def unite(self,a,b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb: return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1
        self.count += 1

uf_road = UnionFind(N)
for a,b in roads:
    if uf_road.is_same(a,b): continue
    uf_road.unite(a,b)

uf_rail = UnionFind(N)
for a,b in rails:
    if uf_rail.is_same(a,b): continue
    uf_rail.unite(a,b)

for i in range(N):
    uf_road.root(i)
    uf_rail.root(i)

road_sets = [None] * N
for i in range(N):
    r = uf_road.root(i)
    if not road_sets[r]:
        road_sets[r] = set()
    road_sets[r].add(i)

rail_sets = [None] * N
for i in range(N):
    r = uf_rail.root(i)
    if not rail_sets[r]:
        rail_sets[r] = set()
    rail_sets[r].add(i)

ans = []
for i in range(N):
    r1 = uf_road.root(i)
    r2 = uf_rail.root(i)
    cnt = len(road_sets[r1] & rail_sets[r2])
    ans.append(cnt)
print(*ans)

Fixed submission:
from collections import Counter
N,K,L = map(int,input().split())

p_road = [i for i in range(N)]
p_rail = [i for i in range(N)]

def root(a,par):
    if a == par[a]: return a
    par[a] = root(par[a],par)
    return par[a]

def unite(a,b,par):
    ra = root(a,par)
    rb = root(b,par)
    if ra == rb: return
    par[ra] = rb

for i in range(K):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_road)

for i in range(L):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_rail)

road_ids = []
rail_ids = []
for i in range(N):
    road_ids.append(root(i,p_road))
    rail_ids.append(root(i,p_rail))

ctr = Counter()
for a,b in zip(road_ids, rail_ids):
    ctr.update([(a,b)])

print(' '.join(list(map(str, [ctr[(road_ids[i], rail_ids[i])] for i in range(N)]))))

Buggy python submission (verdict: Time Limit Exceeded):
N,K,L = map(int,input().split())
roads = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(K)]
rails = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(L)]

class UnionFind:
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.count = 0
    def root(self,a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]
    def is_same(self,a,b):
        return self.root(a) == self.root(b)
    def unite(self,a,b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb: return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1
        self.count += 1

uf_road = UnionFind(N)
for a,b in roads:
    if uf_road.is_same(a,b): continue
    uf_road.unite(a,b)

uf_rail = UnionFind(N)
for a,b in rails:
    if uf_rail.is_same(a,b): continue
    uf_rail.unite(a,b)

for i in range(N):
    uf_road.root(i)
    uf_rail.root(i)

road_sets = [None] * N
for i in range(N):
    r = uf_road.root(i)
    if not road_sets[r]:
        road_sets[r] = set()
    road_sets[r].add(i)

rail_sets = [None] * N
for i in range(N):
    r = uf_rail.root(i)
    if not rail_sets[r]:
        rail_sets[r] = set()
    rail_sets[r].add(i)

ans = []
for i in range(N):
    r1 = uf_road.root(i)
    r2 = uf_rail.root(i)
    cnt = len(road_sets[r1] & rail_sets[r2])
    ans.append(cnt)
print(*ans)

Fixed submission:
from collections import Counter
N,K,L = map(int,input().split())

p_road = [i for i in range(N)]
p_rail = [i for i in range(N)]

def root(a,par):
    if a == par[a]: return a
    par[a] = root(par[a],par)
    return par[a]

def unite(a,b,par):
    ra = root(a,par)
    rb = root(b,par)
    if ra == rb: return
    par[ra] = rb

for i in range(K):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_road)

for i in range(L):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_rail)

road_ids = []
rail_ids = []
for i in range(N):
    road_ids.append(root(i,p_road))
    rail_ids.append(root(i,p_rail))

ctr = Counter()
for a,b in zip(road_ids, rail_ids):
    ctr.update([(a,b)])

print(' '.join(list(map(str, [ctr[(road_ids[i], rail_ids[i])] for i in range(N)]))))

Buggy python submission (verdict: Time Limit Exceeded):
class Sets:
  def __init__(self):
    self.sets = []
  def get_group(self, a):
    if a < 0: return -1
    for i in range(len(self.sets)):
      if a in self.sets[i]:
        return i
    return -1
  def add_same_pair(self, a, b):
    ga = self.get_group(a)
    gb = self.get_group(b)
    if ga < 0 and gb < 0:
      self.sets.append(set([a,b]))
    elif ga == gb:
      return
    elif ga >= 0 and gb < 0:
      self.sets[ga].add(b)
    elif ga < 0 and gb >= 0:
      self.sets[gb].add(a)
    else:
      gmin = min(ga,gb)
      gmax = max(ga,gb)
      s = self.sets.pop(gmax)
      self.sets[gmin] = self.sets[gmin].union(s)
  def get_setids(self, n):
    setids = [-1 for i in range(n)]
    for setid in range(len(self.sets)):
      for e in self.sets[setid]:
        setids[e] = setid
    return setids
  def __str__(self):
    return str(self.sets)

N,K,L = map(int, raw_input().split())
roads = [-1 for i in range(N)]
rails = [-1 for i in range(N)]
road_sets = Sets()
rail_sets = Sets()

for i in range(K):
  p,q = map(int, raw_input().split())
  p -= 1
  q -= 1
  road_sets.add_same_pair(p,q)

for i in range(L):
  r,s = map(int, raw_input().split())
  r -= 1
  s -= 1
  rail_sets.add_same_pair(r,s)

road_ids = road_sets.get_setids(N)
rail_ids = rail_sets.get_setids(N)
result = [1 for i in range(N)]
combi_counts = {}
for i in range(N):
  road = road_ids[i]
  rail = rail_ids[i]
  if road < 0 or rail < 0:
    continue
  if (road,rail) in combi_counts:
    combi_counts[(road,rail)] += 1
  else:
    combi_counts[(road,rail)] = 1

for i in range(N):
  road = road_ids[i]
  rail = rail_ids[i]
  if road < 0 or rail < 0:
    continue
  result[i] = combi_counts[(road,rail)]
print ' '.join(map(str, result))

Fixed submission:
from collections import Counter
N,K,L = map(int,input().split())

p_road = [i for i in range(N)]
p_rail = [i for i in range(N)]

def root(a,par):
    if a == par[a]: return a
    par[a] = root(par[a],par)
    return par[a]

def unite(a,b,par):
    ra = root(a,par)
    rb = root(b,par)
    if ra == rb: return
    par[ra] = rb

for i in range(K):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_road)

for i in range(L):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_rail)

road_ids = []
rail_ids = []
for i in range(N):
    road_ids.append(root(i,p_road))
    rail_ids.append(root(i,p_rail))

ctr = Counter()
for a,b in zip(road_ids, rail_ids):
    ctr.update([(a,b)])

print(' '.join(list(map(str, [ctr[(road_ids[i], rail_ids[i])] for i in range(N)]))))

Buggy python submission (verdict: Time Limit Exceeded):
class Sets:
  def __init__(self):
    self.sets = []
  def get_group(self, a):
    if a < 0: return -1
    for i in range(len(self.sets)):
      if a in self.sets[i]:
        return i
    return -1
  def add_same_pair(self, a, b):
    ga = self.get_group(a)
    gb = self.get_group(b)
    if ga < 0 and gb < 0:
      self.sets.append(set([a,b]))
    elif ga == gb:
      return
    elif ga >= 0 and gb < 0:
      self.sets[ga].add(b)
    elif ga < 0 and gb >= 0:
      self.sets[gb].add(a)
    else:
      gmin = min(ga,gb)
      gmax = max(ga,gb)
      s = self.sets.pop(gmax)
      self.sets[gmin] = self.sets[gmin].union(s)
  def __str__(self):
    return str(self.sets)

N,K,L = map(int, raw_input().split())
roads = [-1 for i in range(N)]
rails = [-1 for i in range(N)]
road_sets = Sets()
rail_sets = Sets()

for i in range(K):
  p,q = map(int, raw_input().split())
  p -= 1
  q -= 1
  road_sets.add_same_pair(p,q)

for i in range(L):
  r,s = map(int, raw_input().split())
  r -= 1
  s -= 1
  rail_sets.add_same_pair(r,s)

combi_counts = {}
for i in range(N):
  g_road = road_sets.get_group(i)#roads[i])
  g_rail = rail_sets.get_group(i)#rails[i])
  if g_road < 0 or g_rail < 0:
    continue
  if (g_road,g_rail) in combi_counts:
    combi_counts[(g_road,g_rail)] += 1
  else:
    combi_counts[(g_road,g_rail)] = 1

result = []
for i in range(N):
  g_road = road_sets.get_group(i)
  g_rail = rail_sets.get_group(i) 
  if g_road < 0 or g_rail < 0:
    result.append(1)
  else:
    result.append(combi_counts[(g_road,g_rail)])
print ' '.join(map(str, result))

Fixed submission:
from collections import Counter
N,K,L = map(int,input().split())

p_road = [i for i in range(N)]
p_rail = [i for i in range(N)]

def root(a,par):
    if a == par[a]: return a
    par[a] = root(par[a],par)
    return par[a]

def unite(a,b,par):
    ra = root(a,par)
    rb = root(b,par)
    if ra == rb: return
    par[ra] = rb

for i in range(K):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_road)

for i in range(L):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_rail)

road_ids = []
rail_ids = []
for i in range(N):
    road_ids.append(root(i,p_road))
    rail_ids.append(root(i,p_rail))

ctr = Counter()
for a,b in zip(road_ids, rail_ids):
    ctr.update([(a,b)])

print(' '.join(list(map(str, [ctr[(road_ids[i], rail_ids[i])] for i in range(N)]))))

Buggy python submission (verdict: Runtime Error):
N,K,L = map(int, raw_input().split())
roads = [-1 for i in range(N)]
rails = [-1 for i in range(N)]
same_roads = {}
same_rails = {}

max_road_id = -1
for i in range(K):
  p,q = map(int, raw_input().split())
  p -= 1
  q -= 1
  if roads[p] < 0 and roads[q] < 0:
    max_road_id += 1
    roads[p] = max_road_id
    roads[q] = max_road_id
  elif roads[p] == roads[q]:
    continue
  elif roads[p] >= 0 and roads[q] < 0:
    roads[q] = roads[p]
  elif roads[q] < 0 and roads[q] >= 0:
    roads[p] = roads[q]
  else:
    same_roads[max(roads[p],roads[q])] = min(roads[p],roads[q]) 
distinct_roads = [i for i in range(max_road_id + 1)]
for road_id in range(max_road_id, -1, -1):
  if road_id in same_roads:
    distinct_roads[road_id] = same_roads[road_id]
 
max_rail_id = -1
for i in range(L):
  r,s = map(int, raw_input().split())
  r -= 1
  s -= 1
  if rails[r] < 0 and rails[s] < 0:
    max_rail_id += 1
    rails[r] = max_rail_id
    rails[s] = max_rail_id
  elif rails[r] == rails[s]:
    continue
  elif rails[r] >= 0 and rails[s] < 0:
    rails[s] = rails[r]
  elif rails[r] < 0 and rails[s] >= 0:
    rails[r] = rails[s]
  else:
    same_rails[max(rails[r],rails[s])] = min(rails[r],rails[s])
distinct_rails = [i for i in range(max_rail_id + 1)]
for rail_id in range(max_rail_id, -1, -1):
  if rail_id in same_rails:
    distinct_rails[rail_id] = same_rails[rail_id]

combi_counts = {}
for i in range(N):
  road = roads[i]
  rail = rails[i]
  if road < 0 or rail < 0:
    continue
  if (road,rail) in combi_counts:
    combi_counts[(distinct_roads[road],distinct_rails[rail])] += 1
  else:
    combi_counts[(distinct_roads[road],distinct_rails[rail])] = 1

result = []
for i in range(N):
  road = roads[i]
  rail = rails[i]
  if road < 0 or rail < 0:
    result.append(1)
  else:
    result.append(combi_counts[(distinct_roads[road],distinct_rails[rail])])
print ' '.join(map(str, result))

Fixed submission:
from collections import Counter
N,K,L = map(int,input().split())

p_road = [i for i in range(N)]
p_rail = [i for i in range(N)]

def root(a,par):
    if a == par[a]: return a
    par[a] = root(par[a],par)
    return par[a]

def unite(a,b,par):
    ra = root(a,par)
    rb = root(b,par)
    if ra == rb: return
    par[ra] = rb

for i in range(K):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_road)

for i in range(L):
    a,b = map(lambda x:int(x)-1, input().split())
    unite(a,b,p_rail)

road_ids = []
rail_ids = []
for i in range(N):
    road_ids.append(root(i,p_road))
    rail_ids.append(root(i,p_rail))

ctr = Counter()
for a,b in zip(road_ids, rail_ids):
    ctr.update([(a,b)])

print(' '.join(list(map(str, [ctr[(road_ids[i], rail_ids[i])] for i in range(N)]))))