Problem:


Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8



Sample input:
3
1 5
3 9
7 8


Sample output:
3

Submission status: Runtime Error

Solution code:
class obj:
    def __init__(self, N):
        self.fa = [i for i in range(N)]
    def find(self, x):
        if x == self.fa[x]:
            return x
        else:
            self.fa[x] = self.find(self.fa[x])
            return self.fa[x]

N = int(input())
node = [{"x":0, "y":0, "id":i} for i in range(N)]
edge = [{"u":0, "v":0, "d":0} for i in range(2*(N-1))]
ob = obj(N)
for i in range(N):
    node[i]["x"], node[i]["y"] = map(int, input().split())
    node[i]["id"] = i

xnode = sorted(node, key=lambda x:x["x"])
for i in range(N-1):
    edge[i]["u"] = xnode[i]["id"]
    edge[i]["v"] = xnode[i+1]["id"]
    edge[i]["d"] = xnode[i+1]["x"] - xnode[i]["x"]
ynode = sorted(node, key=lambda x:x["y"])
for i in range(N-1):
    edge[i+N-1]["u"] = ynode[i]["id"]
    edge[i+N-1]["v"] = ynode[i+1]["id"]
    edge[i+N-1]["d"] = ynode[i+1]["y"] - ynode[i]["y"]
dedge = sorted(edge, key=lambda x:x["d"])
ans = 0
for i in range(2*(N-1)):
    if ob.find(dedge[i]["u"]) != ob.find(dedge[i]["v"]):
        ans += dedge[i]["d"]
        ob.fa[ob.fa[dedge[i]["u"]]] = ob.fa[dedge[i]["v"]]
print(ans)

Submission status: Runtime Error

Solution code:
UF=[i for i in range(100001)]
def u(a,b):
    UF[f(a)] = UF[f(b)]

def f(c):
    if(c==UF[c]):
        return c
    else:
        UF[c]=f(UF[c])
        return UF[c]

n = input()
cities = [map(int,raw_input().split(" "))+[i] for i in range(n)]

cities_x = sorted(cities, key=lambda a:a[0])
cities_y = sorted(cities, key=lambda a:a[1])

road = [[cities_x[i+1][0]-cities_x[i][0],cities_x[i][2],cities_x[i+1][2]] for i in range(n-1)]
road +=[[cities_y[i+1][1]-cities_y[i][1],cities_y[i][2],cities_y[i+1][2]] for i in range(n-1)]

sum = 0
for (cost,i,j) in sorted(road):
    if(f(i)!=f(j)):
        u(i,j)
        sum += cost

print sum

Submission status: Runtime Error

Solution code:
class obj:
    def __init__(self, N):
        self.fa = [i for i in range(N)]
    def find(self, x):
        if x == self.fa[x]:
            return x
        else:
            self.fa[x] = self.find(self.fa[x])
            return self.fa[x]

N = int(input())
node = [{"x":0, "y":0, "id":i} for i in range(N)]
edge = [{"u":0, "v":0, "d":0} for i in range(2*(N-1))]
ob = obj(N)
for i in range(N):
    node[i]["x"], node[i]["y"] = map(int, input().split())
    node[i]["id"] = i

xnode = sorted(node[0:N], key=lambda x:x["x"])
for i in range(N-1):
    edge[i]["u"] = xnode[i]["id"]
    edge[i]["v"] = xnode[i+1]["id"]
    edge[i]["d"] = xnode[i+1]["x"] - xnode[i]["x"]
ynode = sorted(node[0:N], key=lambda x:x["y"])
for i in range(N-1):
    edge[i+N-1]["u"] = ynode[i]["id"]
    edge[i+N-1]["v"] = ynode[i+1]["id"]
    edge[i+N-1]["d"] = ynode[i+1]["y"] - ynode[i]["y"]
dedge = sorted(edge[0:2*N], key=lambda x:x["d"])
ans = 0
for i in range(2*(N-1)):
    if ob.find(dedge[i]["u"]) != ob.find(dedge[i]["v"]):
        ans += dedge[i]["d"]
        ob.fa[ob.fa[dedge[i]["u"]]] = ob.fa[dedge[i]["v"]]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
x=[-1]*(10**9+1)

for i in range(0,N):
    y = list(map(int, input().split()))
    x[y[0]]=y[1]
   
for i in range(N):
    if x[i]!=-1:
        pre=x[i]
        prei=i
        break

res=0

for i in range(N):
    if x[i]!=-1:
        res += min(i-prei,x[i]-pre)
        pre=x[i] 
        prei=i
        
print(res)

quit()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
#平面上に、N 個の街があります。i 個目の街は、座標 (xi,yi) にあります。
#同じ座標に、複数の街があるかもしれません。
#座標 (a,b) にある街と座標 (c,d) にある街の間に道を造るのには、
#min(|a−c|,|b−d|) 円かかります。街と街の間以外に、道を造ることはできません。
#任意の 2つの街の間を、道を何本か通って行き来できるようにするためは、最低で何円必要でしょうか。

N = int(input())

X = []
Y = []


for i in range(N):
    Data = input().split()
    X.append(int(Data[0]))
    Y.append(int(Data[1]))

#N = 6
#X = [8, 4, 12, 18, 13, 7]
#Y = [3, 9 ,19, 1, 5, 6]

D = []
E = []

for i in range(N):
    yen = 1000000001
    for j in range(N):
        
        Ax = X[i]
        Ay = Y[i]
        
        if j != i:
            Bx = X[j]
            By = Y[j]
            
            Cx = abs(Ax - Bx)
            Cy = abs(Ay - By)
            
            if Cx < Cy:
                Check = Cx
            else:
                Check = Cy
            
            if Check < yen:
                yen = Check
                k = j
    
    E.append(i * 10 + k)
    D.append(yen)

        
indexes = [i for i, x in enumerate(E) if x == 1]

Z = len(indexes)

test = 1000000001

for i in range(Z):
    V = D[indexes[i]]
    
    if V < test:
        test = V

 D.remove(V)   


Set = sorted(D)

ans = 0

for i in range(N-1):
    ans = ans + Set[i]


print(ans)