#P2076. 曼哈顿距离最小生成树

    ID: 11559 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树树状数组Special Judge生成树

曼哈顿距离最小生成树

Description

Given nn points (x1,y1),(x2,y2),,(xn,yn)(x_1,y_1),(x_2,y_2),\ldots,(x_n,y_n) in the plane.

Consider the complete graph with nn nodes. For 1u,vn1 \le u, v \le n and uvu \ne v, there is an edge between nodes uu and vv with weight xuxv+yuyv|x_u-x_v|+|y_u-y_v|.

Find a minimum spanning tree of this graph.

Input Format

The first line contains an integer nn, the number of points.

The next nn lines, the ii-th line contains two integers (xi,yi)(x_i,y_i), the coordinates of the ii-th point.

Output Format

The first line contains an integer xx, the sum of edge weights in the minimum spanning tree.

Then (n1)(n-1) lines follow. The ii-th line contains two integers (ui,vi)(u_i,v_i), representing one edge in the minimum spanning tree.

If there are multiple valid answers, you may output any of them.

6
3 8
4 9
2 1
10 5
4 9
2 0
21
5 2
6 3
1 2
3 1
4 1

Hint

For 20%20\% of the testdata, 1n10001 \le n \le 1000.

For 100%100\% of the testdata, 1n2×1051 \le n \le 2 \times 10^5, 0xi,yi1090 \le x_i,y_i \le 10^9.

Translated by ChatGPT 5