Problem p03857 (python)

Fixed submission:
N,K,L=map(int,input().split())
Road_P=[i for i in range(N+1)]
Road_size=[1 for i in range(N+1)]

def find_road(a):
    if a==Road_P[a]:
        return a
    else:
        return find_road(Road_P[a])
        
def union_road(b,c):
    B=find_road(b)
    C=find_road(c)
    if Road_size[B]>=Road_size[C]:
        Road_P[C]=Road_P[B]
        Road_size[B]+=Road_size[C]
    else:
        Road_P[B]=Road_P[C]
        Road_size[C]+=Road_size[B]

for i in range(K):
    p,q=map(int,input().split())
    if find_road(p)!=find_road(q):
        union_road(p,q)
#print(Road_P)

Rail_P=[i for i in range(N+1)]
Rail_size=[1 for i in range(N+1)]

def find_rail(a):
    if a==Rail_P[a]:
        return a
    else:
        return find_rail(Rail_P[a])
        
def union_rail(b,c):
    B=find_rail(b)
    C=find_rail(c)
    if Rail_size[B]>=Rail_size[C]:
        Rail_P[C]=Rail_P[B]
        Rail_size[B]+=Rail_size[C]
    else:
        Rail_P[B]=Rail_P[C]
        Rail_size[C]+=Rail_size[B]

for i in range(L):
    p,q=map(int,input().split())
    if find_rail(p)!=find_rail(q):
        union_rail(p,q)

#print(Rail_P)

comb={}
for i in range(1,N+1):
    if (find_road(i),find_rail(i)) in comb:
        comb[(find_road(i),find_rail(i))]+=1
    else:
        comb[(find_road(i),find_rail(i))]=1

#print(comb)

for i in range(1,N+1):
    print(comb[(find_road(i),find_rail(i))])

Buggy submission (verdict: Runtime Error):
import collections
import bisect
N,K,L=map(int,input().split())

RP=[i for i in range(N+1)]
Rsize=[1 for i in range(N+1)]
TP=[i for i in range(N+1)]
Tsize=[1 for i in range(N+1)]

def findR(a):
    if a==RP[a]:
        return a
    else:
        return findR(RP[a])
        
def findT(a):
    if a==TP[a]:
        return a
    else:
        return findT(TP[a])
        
def unionR(b,c):
    B=findR(b)
    C=findR(c)
    if Rsize[B]>=Rsize[c]:
        Rsize[B]+=Rsize[C]
        RP[C]=RP[B]
    else:
        Rsize[C]+=Rsize[B]
        RP[B]=RP[C]

def unionT(b,c):
    B=findT(b)
    C=findT(c)
    if Tsize[B]>=Tsize[c]:
        Tsize[B]+=Tsize[C]
        TP[C]=TP[B]
    else:
        Tsize[C]+=Tsize[B]
        TP[B]=TP[C]
        
for i in range(K):
    p,q=map(int,input().split())
    if findR(p)==findR(q):
        pass
    else:
        unionR(p,q)

for i in range(L):
    r,s=map(int,input().split())
    if findT(r)==findT(s):
        pass
    else:
        unionT(r,s)
        
L=collections.Counter()

for i in range(1,N+1):
    L[findR(i),findT(i)]+=1
#print(L)

ANS=L.items()
ANS=list(ANS)
ANS.sort()
K=[]
V=[]
for i in range(len(ANS)):
    K.append(ANS[i][0])
    V.append(ANS[i][1])
ans=str(V[bisect.bisect_left(K,(findR(1),findT(1)))])
for i in range(2,N+1):
    ans+=" "+str(V[bisect.bisect_left(K,(findR(i),findT(i)))])
print(ans)

Buggy submission (verdict: Runtime Error):
import collections

N,K,L=map(int,input().split())

RP=[i for i in range(N+1)]
Rsize=[1 for i in range(N+1)]
TP=[i for i in range(N+1)]
Tsize=[1 for i in range(N+1)]

def findR(a):
    if a==RP[a]:
        return a
    else:
        return findR(RP[a])
        
def findT(a):
    if a==TP[a]:
        return a
    else:
        return findT(TP[a])
        
def unionR(b,c):
    B=findR(b)
    C=findR(c)
    if Rsize[B]>=Rsize[c]:
        Rsize[B]+=Rsize[C]
        RP[C]=RP[B]
    else:
        Rsize[C]+=Rsize[B]
        RP[B]=RP[C]

def unionT(b,c):
    B=findT(b)
    C=findT(c)
    if Tsize[B]>=Tsize[c]:
        Tsize[B]+=Tsize[C]
        TP[C]=TP[B]
    else:
        Tsize[C]+=Tsize[B]
        TP[B]=TP[C]
        
for i in range(K):
    p,q=map(int,input().split())
    if findR(p)==findR(q):
        pass
    else:
        unionR(p,q)

for i in range(L):
    r,s=map(int,input().split())
    if findT(r)==findT(s):
        pass
    else:
        unionT(r,s)
        
L=collections.defaultdict(int)
for i in range(1,N+1):
    L[findR(i),findT(i)]+=1

ans=str(L[findR(1),findT(1)])
for i in range(2,N+1):
    ans+=" "+str(L[findR(i),findT(i)])
print(ans)