Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq as hq
input = sys.stdin.readline
n = int(input())
x_xy = []
y_xy = []
for i in range(n):
  x,y = map(int,input().split())
  x_xy.append([x,i+1])
  y_xy.append([y,i+1])
x_xy.sort()
y_xy.sort()
edge = [[] for i in range(n+1)]
for i in range(1,n):
  xf,v2 = x_xy[i]
  xi,v1 = x_xy[i-1]
  edge[v1].append([abs(xf-xi),v2])
  edge[v2].append([abs(xf-xi),v1])
  yf,v2 = y_xy[i]
  yi,v1 = y_xy[i-1]
  edge[v1].append([abs(yf-yi),v2])
  edge[v2].append([abs(yf-yi),v1])
edges = edge[1]
hq.heapify(edges)
connect = [0 for i in range(n+1)]
connect[1] = 1
ans = 0
while edges:
  cost,v2 = hq.heappop(edges)
  if connect[v2] == 1:
    continue
  ans += cost
  connect[v2] = 1
  for e in edge[v2]:
    hq.heappush(edges,e)
print(ans)

Fixed submission:
import sys
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix
input = sys.stdin.readline
n = int(input())
xy = [list(map(int,input().split()))+[i] for i in range(n)]
evv = set()
for Key in range(2):
  xy_s = sorted(xy,key = lambda x:x[Key])
  for i in range(1,n):
    evv.add((xy_s[i][2],xy_s[i-1][2],min(abs(xy_s[i-1][0]-xy_s[i][0]),abs(xy_s[i-1][1]-xy_s[i][1]))))
v1,v2,edge = zip(*evv)
csr = csr_matrix((edge,(v1,v2)),shape = (n,n))
mst = minimum_spanning_tree(csr)
print(int(mst.sum()))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq as hq
input = sys.stdin.readline
n = int(input())
xy = [list(map(int,input().split()))+[i+1] for i in range(n)]
x_xy = sorted(xy)
y_xy = sorted(xy,key = lambda x:x[1])
edge = [[] for i in range(n+1)]
for i in range(1,n):
  xf,yf,v2 = x_xy[i]
  xi,yi,v1 = x_xy[i-1]
  edge[v1].append([abs(xf-xi),v2])
  edge[v2].append([abs(xf-xi),v1])
  xf,yf,v2 = y_xy[i]
  xi,yi,v1 = y_xy[i-1]
  edge[v1].append([abs(yf-yi),v2])
  edge[v2].append([abs(yf-yi),v1])
edges = edge[1]
hq.heapify(edges)
connect = [0 for i in range(n+1)]
connect[1] = 1
ans = 0
while edges:
  cost,v2 = hq.heappop(edges)
  if connect[v2] == 1:
    continue
  ans += cost
  connect[v2] = 1
  for e in edge[v2]:
    hq.heappush(edges,e)
print(ans)

Fixed submission:
import sys
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix
input = sys.stdin.readline
n = int(input())
xy = [list(map(int,input().split()))+[i] for i in range(n)]
evv = set()
for Key in range(2):
  xy_s = sorted(xy,key = lambda x:x[Key])
  for i in range(1,n):
    evv.add((xy_s[i][2],xy_s[i-1][2],min(abs(xy_s[i-1][0]-xy_s[i][0]),abs(xy_s[i-1][1]-xy_s[i][1]))))
v1,v2,edge = zip(*evv)
csr = csr_matrix((edge,(v1,v2)),shape = (n,n))
mst = minimum_spanning_tree(csr)
print(int(mst.sum()))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq as hq
input = sys.stdin.readline
n = int(input())
xy = [list(map(int,input().split()))+[i+1] for i in range(n)]
x_xy = sorted(xy)
y_xy = sorted(xy,key = lambda x:x[1])
edge = [[] for i in range(n+1)]
for i in range(1,n):
  xf,yf,v2 = x_xy[i]
  xi,yi,v1 = x_xy[i-1]
  edge[v1].append([abs(xf-xi),v2])
  edge[v2].append([abs(xf-xi),v1])
  xf,yf,v2 = y_xy[i]
  xi,yi,v1 = y_xy[i-1]
  edge[v1].append([abs(yf-yi),v2])
  edge[v2].append([abs(yf-yi),v1])
edges = edge[1]
hq.heapify(edges)
connect = [0 for i in range(n+1)]
connect[1] = 1
ans = 0
while edges:
  cost,v2 = hq.heappop(edges)
  if connect[v2] == 1:
    continue
  ans += cost
  connect[v2] = 1
  for e in edge[v2]:
    hq.heappush(edges,e)
print(ans)

Fixed submission:
import sys
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix
input = sys.stdin.readline
n = int(input())
xy = [list(map(int,input().split()))+[i] for i in range(n)]
evv = set()
for Key in range(2):
  xy_s = sorted(xy,key = lambda x:x[Key])
  for i in range(1,n):
    evv.add((xy_s[i][2],xy_s[i-1][2],min(abs(xy_s[i-1][0]-xy_s[i][0]),abs(xy_s[i-1][1]-xy_s[i][1]))))
v1,v2,edge = zip(*evv)
csr = csr_matrix((edge,(v1,v2)),shape = (n,n))
mst = minimum_spanning_tree(csr)
print(int(mst.sum()))