1+ import java .io .*;
2+ import java .util .*;
3+
4+ class Node {
5+ int number ;
6+ int x ,y ,z ;
7+ public Node (int number ,int x ,int y ,int z ){
8+ this .number =number ;
9+ this .x =x ;
10+ this .y =y ;
11+ this .z =z ;
12+ }
13+ }
14+
15+ class CNode {
16+ int dist ;
17+ int u ,v ;
18+ public CNode (int dist ,int u ,int v ){
19+ this .dist =dist ;
20+ this .u =u ;
21+ this .v =v ;
22+ }
23+ }
24+
25+ public class P2887 {
26+ public static List <Node > xList =new ArrayList <>();
27+ public static List <Node > yList =new ArrayList <>();
28+ public static List <Node > zList =new ArrayList <>();
29+ public static List <CNode > totalList =new ArrayList <>();
30+
31+ public static int [] parent ;
32+ public static int find (int p ){
33+ if (parent [p ]==p ){
34+ return p ;
35+ }
36+ else {
37+ return find (parent [p ]);
38+ }
39+ }
40+
41+ public static void unionParent (int a ,int b ){
42+ a =find (a );
43+ b =find (b );
44+
45+ if (a <b ){
46+ parent [b ]=a ;
47+ }
48+ else {
49+ parent [a ]=b ;
50+ }
51+ }
52+ public static void main (String [] args ) throws IOException {
53+ BufferedReader br =new BufferedReader (new InputStreamReader (System .in ));
54+ StringTokenizer st =new StringTokenizer (br .readLine ()," " );
55+
56+ int n =Integer .parseInt (st .nextToken ());
57+
58+ for (int i =1 ;i <=n ;i ++){
59+ st =new StringTokenizer (br .readLine ()," " );
60+ int x =Integer .parseInt (st .nextToken ());
61+ int y =Integer .parseInt (st .nextToken ());
62+ int z =Integer .parseInt (st .nextToken ());
63+
64+ Node node =new Node (i ,x ,y ,z );
65+ xList .add (node );
66+ yList .add (node );
67+ zList .add (node );
68+ }
69+
70+ xList .sort (new Comparator <Node >() {
71+ @ Override
72+ public int compare (Node o1 , Node o2 ) {
73+ return o1 .x -o2 .x ;
74+ }
75+ });
76+
77+ for (int i =0 ;i <n -1 ;i ++){
78+ int dist =xList .get (i +1 ).x -xList .get (i ).x ;
79+ CNode node =new CNode (dist ,xList .get (i ).number ,xList .get (i +1 ).number );
80+ totalList .add (node );
81+ }
82+
83+ yList .sort (new Comparator <Node >() {
84+ @ Override
85+ public int compare (Node o1 , Node o2 ) {
86+ return o1 .y -o2 .y ;
87+ }
88+ });
89+
90+ for (int i =0 ;i <n -1 ;i ++){
91+ int dist =yList .get (i +1 ).y -yList .get (i ).y ;
92+ CNode node =new CNode (dist ,yList .get (i ).number ,yList .get (i +1 ).number );
93+ totalList .add (node );
94+ }
95+
96+ zList .sort (new Comparator <Node >() {
97+ @ Override
98+ public int compare (Node o1 , Node o2 ) {
99+ return o1 .z -o2 .z ;
100+ }
101+ });
102+
103+ for (int i =0 ;i <n -1 ;i ++){
104+ int dist =zList .get (i +1 ).z -zList .get (i ).z ;
105+ CNode node =new CNode (dist ,zList .get (i ).number ,zList .get (i +1 ).number );
106+ totalList .add (node );
107+ }
108+
109+ totalList .sort (new Comparator <CNode >() {
110+ @ Override
111+ public int compare (CNode o1 , CNode o2 ) {
112+ return o1 .dist -o2 .dist ;
113+ }
114+ });
115+
116+ long sum =0 ;
117+ parent =new int [n +1 ];
118+ for (int i =1 ;i <=n ;i ++) parent [i ]=i ;
119+ for (int i =0 ;i <totalList .size ();i ++){
120+ CNode e =totalList .get (i );
121+ int u =e .u ;
122+ int v =e .v ;
123+ int dist =e .dist ;
124+
125+ if (find (u )==find (v )) continue ;
126+
127+ unionParent (u ,v );
128+
129+ sum +=dist ;
130+ };
131+ System .out .println (sum );
132+ }
133+ }
0 commit comments