Language: C++

#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
const int oo = 1e9 + 7;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;

int n, par[100001], sz[100001];
vector<iii> point, edg;

int dist(int a, int b, int c, int d){
	return min(abs(a - c), abs(b - d));
}

int anc(int x){
	if(par[x] == -1) return x;
	else return (par[x] = root(par[x]));
}

bool issameset(int x, int y){ return (anc(x) == anc(y));}

void uni(int x, int y){
	x = anc(x);
	y = anc(y);
	if(sz[x] > sz[y]){
		sz[x] += sz[y];
		par[y] = x;
	}
	else{
		sz[y] += sz[x];
		par[x] = y;
	}
}

bool cmp(iii a, iii b){
	return a.se < b.se;
}

signed main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		iii pt;
		cin >> pt.fi.fi >> pt.fi.se;
		pt.second = i;
		point.push_back(pt);
	}
	point.push_back(make_pair(make_pair(-oo, -oo), 0));
	point.push_back(make_pair(make_pair(2 * oo, 2 * oo), 0));
	sort(point.begin(), point.end());
	for(int i = 1; i <= n; i++){
		if(abs(point[i].fi.fi - point[i - 1].fi.fi) > abs(point[i].fi.fi - point[i + 1].fi.fi)) edg.push_back({{point[i].se, point[i + 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i + 1].fi.fi, point[i + 1].fi.se)});
		else if(abs(point[i].fi.fi - point[i - 1].fi.fi) < abs(point[i].fi.fi - point[i + 1].fi.fi)) edg.push_back({{point[i].se, point[i - 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i - 1].fi.fi, point[i - 1].fi.se)});
		else{
			if(abs(point[i].fi.se - point[i - 1].fi.se) > abs(point[i].fi.se - point[i + 1].fi.se)) edg.push_back({{point[i].se, point[i + 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i + 1].fi.fi, point[i + 1].fi.se)});
			else edg.push_back({{point[i].se, point[i - 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i - 1].fi.fi, point[i - 1].fi.se)});
		}
	}
	for(int i = 1; i <= n; i++) swap(point[i].fi.fi, point[i].fi.se);
	sort(point.begin(), point.end());
	for(int i = 1; i <= n; i++){
		if(abs(point[i].fi.fi - point[i - 1].fi.fi) > abs(point[i].fi.fi - point[i + 1].fi.fi)) edg.push_back({{point[i].se, point[i + 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i + 1].fi.fi, point[i + 1].fi.se)});
		else if(abs(point[i].fi.fi - point[i - 1].fi.fi) < abs(point[i].fi.fi - point[i + 1].fi.fi)) edg.push_back({{point[i].se, point[i - 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i - 1].fi.fi, point[i - 1].fi.se)});
		else{
			if(abs(point[i].fi.se - point[i - 1].fi.se) > abs(point[i].fi.se - point[i + 1].fi.se)) edg.push_back({{point[i].se, point[i + 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i + 1].fi.fi, point[i + 1].fi.se)});
			else edg.push_back({{point[i].se, point[i - 1].se}, dist(point[i].fi.fi, point[i].fi.se, point[i - 1].fi.fi, point[i - 1].fi.se)});
		}
	}
	for(int i = 1; i <= n; i++) par[i] = -1;
	sort(edg.begin(), edg.end(), cmp);
	int ans = 0;
	for(int i = 0; i < edg.size(); i++){
	    //cout << edg[i].fi.fi << " " << edg[i].fi.se << " " << edg[i].se << endl;
	    if(!issameset(edg[i].fi.fi, edg[i].fi.se)){
	        ans += edg[i].se;
	        uni(edg[i].fi.fi, edg[i].fi.se);
	    }
	}
	cout << ans;
}

Compiler output:
a.cc: In function 'long long int anc(long long int)':
a.cc:20:31: error: 'root' was not declared in this scope
   20 |         else return (par[x] = root(par[x]));
      |                               ^~~~