Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import heapify, heappush, heappop


class PrimNode:

    def __init__(self, index, cost):
        self.index = index
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost


def prim(N, adj_nodes, costs, initial=0):
    frontier = [PrimNode(initial, 0)]
    heapify(frontier)
    explored = [0 for _ in range(N)]
    mst_cost = 0
    while len(frontier) > 0:
        node = heappop(frontier)
        if explored[node.index] == 1:
            continue
        mst_cost += node.cost
        explored[node.index] = 1
        next_node_indices = adj_nodes[node.index]
        for next_node_index in next_node_indices:
            if explored[next_node_index] == 1:
                continue
            heappush(frontier, PrimNode(next_node_index, costs[(node.index, next_node_index)]))
    return mst_cost


class Point:

    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    adj_nodes = defaultdict(list)
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = cost
        costs[(next_p.index, p.index)] = cost
        costs_defined[(p.index, next_p.index)] = 1
        costs_defined[(next_p.index, p.index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        current_cost = costs[(p.index, next_p.index)]
        if costs_defined[(p.index, next_p.index)]:
            new_cost = min([cost, current_cost])
            costs[(p.index, next_p.index)] = new_cost
            costs[(next_p.index, p.index)] = new_cost
        else:
            costs[(p.index, next_p.index)] = cost
            costs[(next_p.index, p.index)] = cost  
    return adj_nodes, costs


def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    adj_nodes, costs = create_graph(N, points)
    ans = prim(N, adj_nodes, costs)
    print(ans)


if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree


class Point:
 
    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    # Caclulate costs
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        costs[(min_index, max_index)] = cost
        costs_defined[(min_index, max_index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        current_cost = costs[(min_index, max_index)]
        if costs_defined[(min_index, max_index)]:
            new_cost = min([cost, current_cost])
            costs[(min_index, max_index)] = new_cost
        else:
            costs[(min_index, max_index)] = cost
    # Create csgraph
    rows = list()
    cols = list()
    data = list()
    for node_pair, cost in costs.items():
        rows.append(node_pair[0])
        cols.append(node_pair[1])
        data.append(cost)
    return csr_matrix((data, (rows, cols)), shape=(N, N))

 
def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    graph = create_graph(N, points)
    mst = minimum_spanning_tree(graph)
    print(int(mst.sum()))
 
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import heapify, heappush, heappop


class PrimNode:

    def __init__(self, index, cost):
        self.index = index
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost


def prim(N, adj_nodes, costs, initial=0):
    frontier = [PrimNode(initial, 0)]
    heapify(frontier)
    explored = [0 for _ in range(N)]
    mst_cost = 0
    while len(frontier) > 0:
        node = heappop(frontier)
        if explored[node.index] == 1:
            continue
        mst_cost += node.cost
        explored[node.index] = 1
        next_node_indices = adj_nodes[node.index]
        for next_node_index in next_node_indices:
            if explored[next_node_index] == 1:
                continue
            heappush(frontier, PrimNode(next_node_index, costs[node.index][next_node_index]))
    return mst_cost


class Point:

    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    L = 10**12  # large number
    adj_nodes = [[] for _ in range(N)]
    costs = [[L for _ in range(N)] for _ in range(N)]
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[p.index][next_p.index] = cost
        costs[next_p.index][p.index] = cost
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[p.index][next_p.index] = min([cost, costs[p.index][next_p.index]])
        costs[next_p.index][p.index] = min([cost, costs[next_p.index][p.index]])
    return adj_nodes, costs


def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    adj_nodes, costs = create_graph(N, points)
    ans = prim(N, adj_nodes, costs)
    print(ans)


if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree


class Point:
 
    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    # Caclulate costs
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        costs[(min_index, max_index)] = cost
        costs_defined[(min_index, max_index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        current_cost = costs[(min_index, max_index)]
        if costs_defined[(min_index, max_index)]:
            new_cost = min([cost, current_cost])
            costs[(min_index, max_index)] = new_cost
        else:
            costs[(min_index, max_index)] = cost
    # Create csgraph
    rows = list()
    cols = list()
    data = list()
    for node_pair, cost in costs.items():
        rows.append(node_pair[0])
        cols.append(node_pair[1])
        data.append(cost)
    return csr_matrix((data, (rows, cols)), shape=(N, N))

 
def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    graph = create_graph(N, points)
    mst = minimum_spanning_tree(graph)
    print(int(mst.sum()))
 
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import heapify, heappush, heappop


class PrimNode:

    def __init__(self, index, cost):
        self.index = index
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost


def prim(N, adj_nodes, costs, initial=0):
    frontier = [PrimNode(initial, 0)]
    heapify(frontier)
    explored = [0 for _ in range(N)]
    mst_cost = 0
    while len(frontier) > 0:
        node = heappop(frontier)
        if explored[node.index] == 1:
            continue
        mst_cost += node.cost
        explored[node.index] = 1
        next_node_indices = adj_nodes[node.index]
        for next_node_index in next_node_indices:
            if explored[next_node_index] == 1:
                continue
            heappush(frontier, PrimNode(next_node_index, costs[(node.index, next_node_index)]))
    return mst_cost


class Point:

    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    L = 10**12  # large number
    adj_nodes = defaultdict(list)
    costs = defaultdict(lambda:L)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = cost
        costs[(next_p.index, p.index)] = cost
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = min([cost, costs[(p.index, next_p.index)]])
        costs[(next_p.index, p.index)] = min([cost, costs[(next_p.index, p.index)]])
    return adj_nodes, costs


def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    adj_nodes, costs = create_graph(N, points)
    ans = prim(N, adj_nodes, costs)
    print(ans)


if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree


class Point:
 
    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    # Caclulate costs
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        costs[(min_index, max_index)] = cost
        costs_defined[(min_index, max_index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        current_cost = costs[(min_index, max_index)]
        if costs_defined[(min_index, max_index)]:
            new_cost = min([cost, current_cost])
            costs[(min_index, max_index)] = new_cost
        else:
            costs[(min_index, max_index)] = cost
    # Create csgraph
    rows = list()
    cols = list()
    data = list()
    for node_pair, cost in costs.items():
        rows.append(node_pair[0])
        cols.append(node_pair[1])
        data.append(cost)
    return csr_matrix((data, (rows, cols)), shape=(N, N))

 
def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    graph = create_graph(N, points)
    mst = minimum_spanning_tree(graph)
    print(int(mst.sum()))
 
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import heappush, heappop


class PrimNode:

    def __init__(self, index, cost):
        self.index = index
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost


def prim(N, adj_nodes, costs, initial=0):
    frontier = [PrimNode(initial, 0)]
    explored = [0 for _ in range(N)]
    mst_cost = 0
    while len(frontier) > 0:
        node = heappop(frontier)
        if explored[node.index] == 1:
            continue
        mst_cost += node.cost
        explored[node.index] = 1
        next_node_indices = adj_nodes[node.index]
        for next_node_index in next_node_indices:
            if explored[next_node_index] == 1:
                continue
            heappush(frontier, PrimNode(next_node_index, costs[(node.index, next_node_index)]))
    return mst_cost


class Point:

    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    L = 10**12  # large number
    adj_nodes = defaultdict(list)
    costs = defaultdict(lambda:L)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = cost
        costs[(next_p.index, p.index)] = cost
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        adj_nodes[p.index].append(next_p.index)
        adj_nodes[next_p.index].append(p.index)
        costs[(p.index, next_p.index)] = min([cost, costs[(p.index, next_p.index)]])
        costs[(next_p.index, p.index)] = min([cost, costs[(next_p.index, p.index)]])
    return adj_nodes, costs


def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    adj_nodes, costs = create_graph(N, points)
    ans = prim(N, adj_nodes, costs)
    print(ans)


if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree


class Point:
 
    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    # Caclulate costs
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        costs[(min_index, max_index)] = cost
        costs_defined[(min_index, max_index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        current_cost = costs[(min_index, max_index)]
        if costs_defined[(min_index, max_index)]:
            new_cost = min([cost, current_cost])
            costs[(min_index, max_index)] = new_cost
        else:
            costs[(min_index, max_index)] = cost
    # Create csgraph
    rows = list()
    cols = list()
    data = list()
    for node_pair, cost in costs.items():
        rows.append(node_pair[0])
        cols.append(node_pair[1])
        data.append(cost)
    return csr_matrix((data, (rows, cols)), shape=(N, N))

 
def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    graph = create_graph(N, points)
    mst = minimum_spanning_tree(graph)
    print(int(mst.sum()))
 
 
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import heappush, heappop


class PrimNode:

    def __init__(self, index, cost):
        self.index = index
        self.cost = cost

    def __lt__(self, other):
        return self.cost < other.cost


def prim(N, adj_nodes, costs, initial=0):
    frontier = [PrimNode(initial, 0)]
    explored = [0 for _ in range(N)]
    mst_cost = 0
    while len(frontier) > 0:
        node = heappop(frontier)
        if explored[node.index] == 1:
            continue
        mst_cost += node.cost
        explored[node.index] = 1
        next_node_indices = adj_nodes[node.index]
        for next_node_index in next_node_indices:
            if explored[next_node_index] == 1:
                continue
            heappush(frontier, PrimNode(next_node_index, costs[(node.index, next_node_index)]))
    return mst_cost


class Point:

    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    L = 10**12  # large number
    adj_nodes = defaultdict(set)
    costs = defaultdict(lambda:L)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        adj_nodes[p.index].add(next_p.index)
        adj_nodes[next_p.index].add(p.index)
        costs[(p.index, next_p.index)] = cost
        costs[(next_p.index, p.index)] = cost
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        adj_nodes[p.index].add(next_p.index)
        adj_nodes[next_p.index].add(p.index)
        costs[(p.index, next_p.index)] = min([cost, costs[(p.index, next_p.index)]])
        costs[(next_p.index, p.index)] = min([cost, costs[(next_p.index, p.index)]])
    return adj_nodes, costs


def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    adj_nodes, costs = create_graph(N, points)
    ans = prim(N, adj_nodes, costs)
    print(ans)


if __name__ == '__main__':
    main()

Fixed submission:
from collections import defaultdict
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree


class Point:
 
    def __init__(self, index, x, y):
        self.index = index
        self.x = x
        self.y = y


def create_graph(N, points):
    # Caclulate costs
    costs = defaultdict(int)
    costs_defined = defaultdict(int)
    # Sort by `x` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.x)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.x - p.x
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        costs[(min_index, max_index)] = cost
        costs_defined[(min_index, max_index)] = 1
    # Sort by `y` and create edges between adjacent nodes
    points = sorted(points, key=lambda p: p.y)
    for i in range(N - 1):
        p = points[i]
        next_p = points[i + 1]
        cost = next_p.y - p.y
        min_index, max_index = min([p.index, next_p.index]), max([p.index, next_p.index])
        current_cost = costs[(min_index, max_index)]
        if costs_defined[(min_index, max_index)]:
            new_cost = min([cost, current_cost])
            costs[(min_index, max_index)] = new_cost
        else:
            costs[(min_index, max_index)] = cost
    # Create csgraph
    rows = list()
    cols = list()
    data = list()
    for node_pair, cost in costs.items():
        rows.append(node_pair[0])
        cols.append(node_pair[1])
        data.append(cost)
    return csr_matrix((data, (rows, cols)), shape=(N, N))

 
def main():
    N = int(input())
    points = list()
    for index in range(N):
        x, y = list(map(int, input().split(' ')))
        points.append(Point(index, x, y))
    graph = create_graph(N, points)
    mst = minimum_spanning_tree(graph)
    print(int(mst.sum()))
 
 
if __name__ == '__main__':
    main()