Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
p = [list(map(int,input().split())) for _ in range(n)]

X = sorted([(i, xy[0]) for i, xy in enumerate(p)], key=lambda x: x[1])
Y = sorted([(i, xy[1]) for i, xy in enumerate(p)], key=lambda x: x[1])
dX = [[X[i][0], X[i+1][0], X[i+1][1]-X[i][1]] for i in range(n-1)]
dY = [[Y[i][0], Y[i+1][0], Y[i+1][1]-Y[i][1]] for i in range(n-1)]
D = sorted(dX+dY, key=lambda x: x[2])

def root(x):
    if parent[x] == x:
        return x
    else:
        return root(parent[x])

def connected(x, y):
    return root(x) == root(y)

parent = [i for i in range(n+1)]
depth = [1 for i in range(n+1)]

ans = 0

while D:
    a, b, d = D.pop(0)
    ra, rb = map(root, [a,b])
    if ra != rb:
        if depth[ra] < depth[rb]:
            a, b = b, a
            ra, rb = rb, ra
        parent[rb] = ra
        depth[ra] = max(depth[ra], depth[rb]+1)
        ans += d
print(ans)


Fixed submission:
n = int(input())
p = [list(map(int,input().split())) for _ in range(n)]

X = sorted([(i, xy[0]) for i, xy in enumerate(p)], key=lambda x: x[1])
Y = sorted([(i, xy[1]) for i, xy in enumerate(p)], key=lambda x: x[1])
dX = sorted([[X[i][0], X[i+1][0], X[i+1][1]-X[i][1]] for i in range(n-1)], key=lambda x: x[2])
dY = sorted([[Y[i][0], Y[i+1][0], Y[i+1][1]-Y[i][1]] for i in range(n-1)], key=lambda x: x[2])

def root(x):
    if parent[x] == x:
        return x
    else:
        return root(parent[x])

def connected(x, y):
    return root(x) == root(y)

parent = [i for i in range(n+1)]
depth = [1 for i in range(n+1)]

ans = 0
i, j = 0, 0
while i < n-1 and j < n-1:
    if dX[i][2] < dY[j][2]:
        a, b, d = dX[i]
        i += 1
    else:
        a, b, d = dY[j]
        j += 1
    ra, rb = map(root, [a,b])
    if ra != rb:
        if depth[ra] < depth[rb]:
            a, b = b, a
            ra, rb = rb, ra
        parent[rb] = ra
        depth[ra] = max(depth[ra], depth[rb]+1)
        ans += d
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
p = [list(map(int,input().split())) for _ in range(n)]

X = sorted([(i, xy[0]) for i, xy in enumerate(p)], key=lambda x: x[1])
Y = sorted([(i, xy[1]) for i, xy in enumerate(p)], key=lambda x: x[1])
dX = [[X[i][0], X[i+1][0], X[i+1][1]-X[i][1]] for i in range(n-1)]
dY = [[Y[i][0], Y[i+1][0], Y[i+1][1]-Y[i][1]] for i in range(n-1)]
D = sorted(dX+dY, key=lambda x: x[2])

def root(x):
    if parent[x] == x:
        return x
    else:
        return root(parent[x])

def connected(x, y):
    return root(x) == root(y)

parent = [i for i in range(n+1)]
ans = 0

while D:
    a, b, d = D.pop(0)
    if not connected(a, b):
        parent[root(b)] = root(a)
        ans += d
print(ans)


Fixed submission:
n = int(input())
p = [list(map(int,input().split())) for _ in range(n)]

X = sorted([(i, xy[0]) for i, xy in enumerate(p)], key=lambda x: x[1])
Y = sorted([(i, xy[1]) for i, xy in enumerate(p)], key=lambda x: x[1])
dX = sorted([[X[i][0], X[i+1][0], X[i+1][1]-X[i][1]] for i in range(n-1)], key=lambda x: x[2])
dY = sorted([[Y[i][0], Y[i+1][0], Y[i+1][1]-Y[i][1]] for i in range(n-1)], key=lambda x: x[2])

def root(x):
    if parent[x] == x:
        return x
    else:
        return root(parent[x])

def connected(x, y):
    return root(x) == root(y)

parent = [i for i in range(n+1)]
depth = [1 for i in range(n+1)]

ans = 0
i, j = 0, 0
while i < n-1 and j < n-1:
    if dX[i][2] < dY[j][2]:
        a, b, d = dX[i]
        i += 1
    else:
        a, b, d = dY[j]
        j += 1
    ra, rb = map(root, [a,b])
    if ra != rb:
        if depth[ra] < depth[rb]:
            a, b = b, a
            ra, rb = rb, ra
        parent[rb] = ra
        depth[ra] = max(depth[ra], depth[rb]+1)
        ans += d
print(ans)