Buggy python submission (verdict: Time Limit Exceeded):

def want_p(n):

    Q = []

    while n != p[n]:
        Q.append(n)
        n = p[n]

    for i in Q:
        p[i] = n

    return n


N = int(input())

xyi = []
yxi = []

for i in range(N):

    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])

xyi.sort()
yxi.sort()

line = []


for i in range(N-1):

    line.append([ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    line.append([ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])

line.sort()

p = [i for i in range(N)]
rank = [1] * N
ans = 0

for j in range(len(line)):

    e = line[j]
    cost = e[0]
    a = e[1]
    b = e[2]

    ap = want_p(a)
    bp = want_p(b)

    #print (a,b,cost,p,ans)

    if ap != bp:
        ans += cost

        if rank[ap] > rank[bp]:
            p[bp] = ap

        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1

        else:
            p[ap] = bp

print (ans)

Fixed submission:
def want_p(n):
 
    Q = []
 
    while n != p[n]:
        Q.append(n)
        n = p[n]
 
    for i in Q:
        p[i] = n
 
    return n
 
 
N = int(input())
 
xyi = []
yxi = []
 
for i in range(N):
 
    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])
 
xyi.sort()
yxi.sort()
 
line = []
 
 
for i in range(N-1):
 
    line.append([ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    line.append([ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])
 
line.sort()
 
p = [i for i in range(N)]
rank = [1] * N
num = [1] * N
ans = 0
 
for j in range(len(line)):
 
    e = line[j]
    cost = e[0]
    a = e[1]
    b = e[2]
 
    ap = want_p(a)
    bp = want_p(b)
 
    if ap != bp:
        ans += cost

        k = num[ap]
        num[ap] += num[bp]
        num[bp] += k

        if num[ap] == N or num[bp] == N:
            break
 
        if rank[ap] > rank[bp]:
            p[bp] = ap
 
        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1
 
        else:
            p[ap] = bp
 
print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

def want_p(n):

    Q = []

    while n != p[n]:
        Q.append(n)
        n = p[n]

    for i in Q:
        p[i] = n

    return n


N = int(input())

xyi = []
yxi = []

for i in range(N):

    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])

xyi.sort()
yxi.sort()

line = []


for i in range(N-1):

    line.append([ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    line.append([ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])

line.sort()

p = [i for i in range(N)]
rank = [1] * N
ans = 0

for j in range(len(line)):

    e = line[j]
    cost = e[0]
    a = e[1]
    b = e[2]

    ap = want_p(a)
    bp = want_p(b)

    #print (a,b,cost,p,ans)

    if ap != bp:
        ans += cost

        if rank[ap] > rank[bp]:
            p[bp] = ap

        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1

        else:
            p[ap] = bp

print (ans)

Fixed submission:
def want_p(n):
 
    Q = []
 
    while n != p[n]:
        Q.append(n)
        n = p[n]
 
    for i in Q:
        p[i] = n
 
    return n
 
 
N = int(input())
 
xyi = []
yxi = []
 
for i in range(N):
 
    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])
 
xyi.sort()
yxi.sort()
 
line = []
 
 
for i in range(N-1):
 
    line.append([ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    line.append([ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])
 
line.sort()
 
p = [i for i in range(N)]
rank = [1] * N
num = [1] * N
ans = 0
 
for j in range(len(line)):
 
    e = line[j]
    cost = e[0]
    a = e[1]
    b = e[2]
 
    ap = want_p(a)
    bp = want_p(b)
 
    if ap != bp:
        ans += cost

        k = num[ap]
        num[ap] += num[bp]
        num[bp] += k

        if num[ap] == N or num[bp] == N:
            break
 
        if rank[ap] > rank[bp]:
            p[bp] = ap
 
        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1
 
        else:
            p[ap] = bp
 
print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

def want_p(n):

    Q = []

    while n != p[n]:
        Q.append(n)
        n = p[n]

    for i in Q:
        p[i] = n

    return n


N = int(input())

xyi = []
yxi = []

for i in range(N):

    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])

xyi.sort()
yxi.sort()

line = []


for i in range(N-1):

    heapq.heappush(line,[ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    heapq.heappush(line,[ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])

line.sort()

p = [i for i in range(N)]
rank = [1] * N
ans = 0

while len(line) > 0:

    e = heapq.heappop(line)
    cost = e[0]
    a = e[1]
    b = e[2]

    ap = want_p(a)
    bp = want_p(b)

    #print (a,b,cost,p,ans)

    if ap != bp:
        ans += cost

        if rank[ap] > rank[bp]:
            p[bp] = ap

        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1

        else:
            p[ap] = bp

print (ans)
        

Fixed submission:
def want_p(n):
 
    Q = []
 
    while n != p[n]:
        Q.append(n)
        n = p[n]
 
    for i in Q:
        p[i] = n
 
    return n
 
 
N = int(input())
 
xyi = []
yxi = []
 
for i in range(N):
 
    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])
 
xyi.sort()
yxi.sort()
 
line = []
 
 
for i in range(N-1):
 
    line.append([ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    line.append([ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])
 
line.sort()
 
p = [i for i in range(N)]
rank = [1] * N
num = [1] * N
ans = 0
 
for j in range(len(line)):
 
    e = line[j]
    cost = e[0]
    a = e[1]
    b = e[2]
 
    ap = want_p(a)
    bp = want_p(b)
 
    if ap != bp:
        ans += cost

        k = num[ap]
        num[ap] += num[bp]
        num[bp] += k

        if num[ap] == N or num[bp] == N:
            break
 
        if rank[ap] > rank[bp]:
            p[bp] = ap
 
        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1
 
        else:
            p[ap] = bp
 
print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

def want_p(n):

    Q = []

    while n != p[n]:
        Q.append(n)
        n = p[n]

    for i in Q:
        p[i] = n

    return n


N = int(input())

xyi = []
yxi = []

for i in range(N):

    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])

xyi.sort()
yxi.sort()

line = []


for i in range(N-1):

    heapq.heappush(line,[ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    heapq.heappush(line,[ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])

line.sort()

p = [i for i in range(N)]
rank = [1] * N
ans = 0

while len(line) > 0:

    e = heapq.heappop(line)
    cost = e[0]
    a = e[1]
    b = e[2]

    ap = want_p(a)
    bp = want_p(b)

    #print (a,b,cost,p,ans)

    if ap != bp:
        ans += cost

        if rank[ap] > rank[bp]:
            p[bp] = ap

        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1

        else:
            p[ap] = bp

print (ans)
        

Fixed submission:
def want_p(n):
 
    Q = []
 
    while n != p[n]:
        Q.append(n)
        n = p[n]
 
    for i in Q:
        p[i] = n
 
    return n
 
 
N = int(input())
 
xyi = []
yxi = []
 
for i in range(N):
 
    x,y = map(int,input().split())
    xyi.append([x,y,i])
    yxi.append([y,x,i])
 
xyi.sort()
yxi.sort()
 
line = []
 
 
for i in range(N-1):
 
    line.append([ xyi[i+1][0]-xyi[i][0] , xyi[i][2] , xyi[i+1][2] ])
    line.append([ yxi[i+1][0]-yxi[i][0] , yxi[i][2] , yxi[i+1][2] ])
 
line.sort()
 
p = [i for i in range(N)]
rank = [1] * N
num = [1] * N
ans = 0
 
for j in range(len(line)):
 
    e = line[j]
    cost = e[0]
    a = e[1]
    b = e[2]
 
    ap = want_p(a)
    bp = want_p(b)
 
    if ap != bp:
        ans += cost

        k = num[ap]
        num[ap] += num[bp]
        num[bp] += k

        if num[ap] == N or num[bp] == N:
            break
 
        if rank[ap] > rank[bp]:
            p[bp] = ap
 
        elif rank[ap] == rank[bp]:
            p[bp] = ap
            rank[ap] += 1
 
        else:
            p[ap] = bp
 
print (ans)