Problem p03682 (python)

Fixed submission:
from operator import itemgetter
from queue import deque

N = int(input())

xin = []
yin = []

for _ in range(N):
    x, y = map(int, input().split())
    xin.append(x)
    yin.append(y)
    
xin = sorted(list(enumerate(xin)), key=itemgetter(1))
yin = sorted(list(enumerate(yin)), key=itemgetter(1))

xd = []
yd = []

for i in range(N-1):
    xd.append((xin[i+1][1] - xin[i][1], (xin[i][0], xin[i+1][0])))
    yd.append((yin[i+1][1] - yin[i][1], (yin[i][0], yin[i+1][0])))
    
xdq = deque(sorted(xd))
ydq = deque(sorted(yd))

par = list(range(N))

def find(x):
    if x == par[x]:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
    
def union(x, y):
    px, py = find(x), find(y)
    if px != py:
        par[py] = px
    
ans = 0

while xdq or ydq:
    if xdq and (not ydq or xdq[0] < ydq[0]):
        d, (i, j) = xdq.popleft()
    else:
        d, (i, j) = ydq.popleft()
        
    if find(i) != find(j):
        ans += d
        union(i, j)
        
print(ans)

Buggy submission (verdict: Runtime Error):
from operator import itemgetter
from queue import deque

N = int(input())

xin = []
yin = []

for _ in range(N):
    x, y = map(int, input().split())
    xin.append(x)
    yin.append(y)
    
xin = sorted(list(enumerate(xin)), key=itemgetter(1))
yin = sorted(list(enumerate(yin)), key=itemgetter(1))

xd = []
yd = []

for i in range(N-1):
    xd.append((xin[i+1][1] - xin[i][1], (xin[i][0], xin[i+1][0])))
    yd.append((yin[i+1][1] - yin[i][1], (yin[i][0], yin[i+1][0])))
    
xdq = deque(sorted(xd))
ydq = deque(sorted(yd))

par = list(range(N))

def find(x):
    if x == par[x]:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
    
def union(x, y):
    px, py = find(x), find(y)
    if px != py:
        par[px] = py
    
ans = 0

while xdq or ydq:
    if xdq and (not ydq or xdq[0] < ydq[0]):
        d, (i, j) = xdq.popleft()
    else:
        d, (i, j) = ydq.popleft()
        
    if find(i) != find(j):
        ans += d
        union(i, j)
        
print(ans)

Buggy submission (verdict: Runtime Error):
from operator import itemgetter
from queue import deque

N = int(input())

xin, yin = [], []
for _ in range(N):
    x, y = map(int, input().split())
    xin.append(x)
    yin.append(y)

x = sorted(list(enumerate(xin)), key=itemgetter(1))
y = sorted(list(enumerate(yin)), key=itemgetter(1))

xds = []
yds = []

for i in range(N-1):
    xds.append((x[i+1][1] - x[i][1],(x[i][0], x[i+1][0])))
    yds.append((y[i+1][1] - y[i][1],(y[i][0], y[i+1][0])))
    
xdq = deque(sorted(xds))
ydq = deque(sorted(yds))

par = list(range(N))

def find(x):
    if x == par[x]:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def union(x, y):
    px, py = find(x), find(y)
    par[px] = py

ans = 0    
while xdq or ydq:
    if xdq and (not ydq or xdq[0] < ydq[0]):
        d, (i, j) = xdq.popleft()
    else:
        d, (i, j) = ydq.popleft()
        
    if find(i) != find(j):
        ans += d
        union(i, j)
print(ans)

Buggy submission (verdict: Runtime Error):
from operator import itemgetter
from queue import deque

N = int(input())

x_in = []
y_in = []

for _ in range(N):
    x, y = list(map(int, input().split(" ")))
    x_in.append(x)
    y_in.append(y)

x_in = sorted(list(enumerate(x_in)), key=itemgetter(1))
y_in = sorted(list(enumerate(y_in)), key=itemgetter(1))

xd = []
yd = []

for i in range(N-1):
    xd.append((x_in[i+1][1]-x_in[i][1], (x_in[i][0], x_in[i+1][0])))
    yd.append((y_in[i+1][1]-y_in[i][1], (y_in[i][0], y_in[i+1][0])))
    
xdq = deque(sorted(xd))
ydq = deque(sorted(yd))

par = list(range(N))

def find(x):
    if x == par[x]:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def union(x, y):
    px, py = find(x), find(y)
    if px != py:
        par[px] = py
        
ans = 0

while xdq or ydq:
    if xdq and (not ydq or xdq[0] < ydq[0]):
        d, (i, j) = xdq.popleft()
    else:
        d, (i, j) = ydq.popleft()
        
    if find(i) != find(j):
        ans += d
        union(i, j)
        
print(ans)