Language: C++

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
long long int n, a[100005], val[100005],set[100005];
int findSet(int x) {
	if (set[x] == x)
		return x;
	return set[x] = findSet(set[x]);
}

void unionSet(int x, int y) {
	int fx = findSet(x);
	int fy = findSet(y);
	if(fx!=fy)
		set[fx] = fy;
}
struct road {
	long long int len,turn1,turn2;
};
struct road rx[100005],ry[100005];
struct retuint {
	long long int x,y,turn;
};
struct retuint retu[100005],retu2[100005];
bool cmp(struct retuint p1, struct retuint p2) {
	if(p1.x<p2.x)	return true;
	if(p1.x>=p2.x)	return false;
}
bool cmp2(struct retuint p1, struct retuint p2) {
	if(p1.y<p2.y)	return true;
	if(p1.y>=p2.y)	return false;
}
long long int fact(long long int val)
{
    long long int te = 1;
    for (long long int i = 1; i <= val; i++)
    {


        te = (te*i);
        te = te%MOD;
    }
    return te;
}


bool cmprx(struct road r1, struct road r2) {
	if(r1.len<r2.len)	return true;
	if(r1.len>=r2.len)	return false;
}
bool cmpry(struct road r1, struct road r2) {
	if(r1.len<r2.len)	return true;
	if(r1.len>=r2.len)	return false;
}
int main() {
	int i,j;
	cin>>n;
	for(int i=0; i<=n; i++)	set[i] =i;
	for(i=0; i<n; i++) {
		cin>>retu[i].x>>retu[i].y;
		retu2[i].x = retu[i].x;
		retu2[i].y = retu[i].y;
		retu[i].turn = i+1;
		retu2[i].turn = i+1;
	}
	sort(retu,retu+n,cmp);
	sort(retu2,retu2+n,cmp2);

	for(i=1; i<n; i++) {
		rx[i-1].turn1 = retu[i-1].turn;
		rx[i-1].turn2 = retu[i].turn;
		rx[i-1].len = fabs(retu[i-1].x - retu[i].x);
	}
	for(i=1; i<n; i++) {
		ry[i-1].turn1 = retu2[i-1].turn;
		ry[i-1].turn2 = retu2[i].turn;
		ry[i-1].len = fabs(retu2[i-1].y - retu2[i].y);
	}
	long long int ans = 0;
	sort(rx,rx+n-1,cmprx);
	sort(ry,ry+n-1,cmpry);

	int fc1=0,fc2=0;
	int tt = 0;
	int xx = 100000;
	for(; tt<n-1; ) {
        if (xx<10000000){
		long long int r1 = rx[fc1].len, r2 = ry[fc2].len;
		if(r1<r2) {
			if(findSet(rx[fc1].turn1)!=findSet(rx[fc1].turn2)) {
				ans+=rx[fc1].len;
				unionSet(rx[fc1].turn1,rx[fc1].turn2);
				tt++;
				xx--;
			}
			fc1++;
			xx++;
		} else {
			if(findSet(ry[fc2].turn1)!=findSet(ry[fc2].turn2)) {
				ans+=ry[fc2].len;
				unionSet(ry[fc2].turn1,ry[fc2].turn2);
				tt++;
				xx--;
			}
			fc2++;
			xx++;
		}
        }
	}

	cout<<ans;
	return 0;
}


Compiler output:
a.cc: In function 'long long int fact(long long int)':
a.cc:42:17: error: 'MOD' was not declared in this scope
   42 |         te = te%MOD;
      |                 ^~~
a.cc: In function 'int main()':
a.cc:73:31: error: 'fabs' was not declared in this scope; did you mean 'labs'?
   73 |                 rx[i-1].len = fabs(retu[i-1].x - retu[i].x);
      |                               ^~~~
      |                               labs
a.cc:78:31: error: 'fabs' was not declared in this scope; did you mean 'labs'?
   78 |                 ry[i-1].len = fabs(retu2[i-1].y - retu2[i].y);
      |                               ^~~~
      |                               labs
a.cc: In function 'bool cmp(retuint, retuint)':
a.cc:29:1: warning: control reaches end of non-void function [-Wreturn-type]
   29 | }
      | ^
a.cc: In function 'bool cmp2(retuint, retuint)':
a.cc:33:1: warning: control reaches end of non-void function [-Wreturn-type]
   33 | }
      | ^
a.cc: In function 'bool cmprx(road, road)':
a.cc:51:1: warning: control reaches end of non-void function [-Wreturn-type]
   51 | }
      | ^
a.cc: In function 'bool cmpry(road, road)':
a.cc:55:1: warning: control reaches end of non-void function [-Wreturn-type]
   55 | }
      | ^