Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[]
        self.dict=defaultdict(lambda:[])
        for i in range(self.n):
            self.roots.append(i)
            self.dict[i].append(i)

    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                #self.dict[self.root(y)]=self.dict[self.root(y)]+self.dict[self.root(x)]
                #self.dict[self.root(x)]=[]
                self.roots[self.root(x)]=self.root(y)
                #self.allunite(self.root(x),self.root(y))
            else:
                #self.dict[self.root(x)]=self.dict[self.root(x)]+self.dict[self.root(y)]
                #self.dict[self.root(y)]=[]
                self.roots[self.root(y)]=self.root(x)
                #self.allunite(self.root(y),self.root(x))
    

    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    count[str(l)]+=1
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    print(count[str(l)],end=" ")


Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[]
        self.dict=defaultdict(lambda:[])

        for i in range(self.n):
            self.roots.append(i)
            self.dict[i].append(i)

    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.dict[self.root(y)]=self.dict[self.root(y)]+self.dict[self.root(x)]
                self.dict[self.root(x)]=[]
                self.roots[self.root(x)]=self.root(y)
                self.allunite(self.root(x),self.root(y))
            else:
                self.dict[self.root(x)]=self.dict[self.root(x)]+self.dict[self.root(y)]
                self.dict[self.root(y)]=[]
                self.roots[self.root(y)]=self.root(x)
                self.allunite(self.root(y),self.root(x))
    def find(self,x):
        return self.dict[x]

    def allunite(self,x,y):
        unit=self.find(x)
        for i in range(len(unit)):
            self.roots[unit[i]]=y
            return
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)


road_g=road.roots
train_g=train.roots

count=defaultdict(lambda:0)
for i in range(N):
    l=[road_g[i],train_g[i]]
    count[str(l)]+=1
for i in range(N):
    l=[road_g[i],train_g[i]]
    print(count[str(l)],end=" ")


Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]
    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
                self.allunite(self.root(x),self.root(y))
            else:
                self.roots[self.root(y)]=self.root(x)
                self.allunite(self.root(y),self.root(x))
    def find(self,x):
        unit=[]
        for i in range(self.n):
            if x==self.roots[i]:
                unit.append(i)
        return unit
    def allunite(self,x,y):
        unit=self.find(x)
        for i in range(len(unit)):
            self.roots[unit[i]]=y
            return
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)


road_g=road.roots
train_g=train.roots

count=defaultdict(lambda:0)
for i in range(N):
    l=[road_g[i],train_g[i]]
    count[str(l)]+=1
for i in range(N):
    l=[road_g[i],train_g[i]]
    print(count[str(l)],end=" ")

Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)

class deep_search:
    def __init__(self,n):
        self.n=n
        self.stack=[]
        self.mem=[]
        self.group={}
        self.dict=defaultdict(lambda:[])

    def unit(self,x,y):
        self.dict[x].append(y)
    def search(self):
        for v in range(1,self.n+1):
            if v not in self.mem:
                self.stack.append(v)
                self.mem.append(v)
                self.group[v]=v
                while self.stack!=[]:
                    w=self.stack.pop()
                    for ww in self.dict[w]:
                        if ww not in self.mem:
                            self.mem.append(ww)
                            self.group[ww]=v
                            self.stack.append(ww)
        return self.group
    
utl=deep_search(N)
utf=deep_search(N)

for i in range(K):
    p,q=map(int,list(input().split(" ")))
    utl.unit(p,q)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    utf.unit(r,s)
road_g=utl.search()
train_g=utf.search()
count=defaultdict(lambda:0)

for i in range(1,N+1):
    l=[road_g[i],train_g[i]]
    count[str(l)]+=1
for i in range(1,N+1):
    l=[road_g[i],train_g[i]]
    print(count[str(l)],end=" ")

Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=defaultdict(lambda:[])
train=defaultdict(lambda:[])
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road[p].append(q)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train[r].append(s)

def deep_search(dict):
    stack=[] 
    mem=[]
    group={}
    for v in list(dict):
        if v not in mem:
            stack.append(v)
            while stack!=[]:
                w=stack.pop()
                mem.append(w)
                group[w]=v
                for ww in dict[w]:
                    if ww not in mem:
                        mem.append(ww)
                        group[ww]=v
                        stack.append(ww)
    return group

road_g=deep_search(road)
train_g=deep_search(train)
count=defaultdict(lambda:0)
for i in range(1,N+1):
    if i in road_g.keys() and i in train_g.keys():
        l=[road_g[i],train_g[i]]
        count[str(l)]+=1
for i in range(1,N+1):
    if i in road_g.keys() and i in train_g.keys():
        l=[road_g[i],train_g[i]]
        print(count[str(l)],end=" ")
    else:
        print(1,end=" ")

Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
def deep_search(L,i,f):
    Q=L[i]
    if f[i]==0:
        f[i]=i
        for j in L[i]:
            f[j]=i
        while Q!=[]:
            p=Q.pop()
            Q=Q+L[p]
            for j in L[p]:
                f[j]=i
    return f

N,K,L=input().split(" ")
N=int(N)
K=int(K)
L=int(L)
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
road=[[] for i in range(N+1)]
train=[[] for i in range(N+1)]
for i in range(K+L):
    p,q=input().split(" ")
    p=int(p)
    q=int(q)
    if i<K:
        road[p].append(q)
    else:
        train[p].append(q)
for i in range(1,N+1):
    a=deep_search(road,i,a)
    b=deep_search(train,i,b)
ans=[[a[i],b[i]] for i in range(1,N+1)]
for i in range(N):
    print(ans.count(ans[i]),end=" ")

Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")

Buggy python submission (verdict: Time Limit Exceeded):
def deep_search(L,i,f):
    Q=L[i]
    if f[i]==0:
        f[i]=i
        for j in L[i]:
            f[j]=i
        while Q!=[]:
            p=Q.pop()
            Q=Q+L[p]
            for j in L[p]:
                f[j]=i
    return f

N,K,L=input().split(" ")
N=int(N)
K=int(K)
L=int(L)
a=[0 for i in range(N+1)]
b=[0 for i in range(N+1)]
road=[[] for i in range(N+1)]
train=[[] for i in range(N+1)]
for i in range(K+L):
    p,q=input().split(" ")
    p=int(p)
    q=int(q)
    if i<K:
        road[p].append(q)
    else:
        train[p].append(q)
for i in range(1,N+1):
    a=deep_search(road,i,a)
    b=deep_search(train,i,b)
ans=[]
for i in range(1,N+1):
    time=0
    for j in range(1,N+1):
        if a[i]==a[j] and b[i]==b[j]:
            time+=1
    print(time,end=" ")



Fixed submission:
from collections import defaultdict
class Unionfind:
    def __init__(self,n):
        self.n=n
        self.roots=[i for i in range(self.n)]


    def root(self,x):
        if self.roots[x]==x:
            return x
        else:
            self.roots[x]=self.root(self.roots[x])
            return self.roots[x]
    def unite(self,x,y):
        if self.root(x)==self.root(y):
            return
        else:
            if self.root(x)>self.root(y):
                self.roots[self.root(x)]=self.root(y)
            else:
                self.roots[self.root(y)]=self.root(x)
    
    def same(self,x,y):
        return self.root(x)==self.root(y)

N,K,L=list(input().split(" "))
N=int(N)
K=int(K)
L=int(L)
road=Unionfind(N)
train=Unionfind(N)
for i in range(K):
    p,q=map(int,list(input().split(" ")))
    road.unite(p-1,q-1)
for i in range(L):
    r,s=map(int,list(input().split(" ")))
    train.unite(r-1,s-1)



count=defaultdict(lambda:0)
point=[]
for i in range(N):
    a=road.root(i)
    b=train.root(i)
    l=[a,b]
    point.append(l)
    count[str(l)]+=1
for i in point:
    print(count[str(i)],end=" ")