stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include <stdio.h>

#include<algorithm>

#include<cstring>

#include<cmath>

#include<cstdlib>

#include<queue>

#include<map>

#include<vector>

#include <set>

#define ll long long

using namespace std;

const int maxn=5e5;

int s[maxn];

vector <int> v[200010];

	queue <int> p;

void bfs(){

		while(!p.empty()){

		int x=p.front();

		p.pop();

		for(int i=0;i<v[x].size();i++){

//			cout<<v[x][i]<<" "; 

			if(s[v[x][i]]==0&&v[x][i]!=1){

				p.push(v[x][i]);

				s[v[x][i]]=x;

			}

		}

	}

} 

int main(){

	int n,m;

	cin>>n>>m;

	for(int i=0;i<m;i++){

		int a,b;

		cin>>a>>b;

		v[a].push_back(b);

		v[b].push_back(a);

	}



	p.push(1);

	memset(s,0,sizeof(s));

	s[1]=1;

    bfs();

	int l=1;

	for(int i=1;i<=n;i++){

//		cout<<s[i]<<endl;

		if(s[i]==0)

		{  l=0;

		   break;

		}

	}

	if(l){

		cout<<"Yes"<<endl;

		for(int i=2;i<=n;i++)

		cout<<s[i]<<endl;

	}

	else 

	cout<<"No"<<endl;

	

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<cstdio>

#include<string.h>

#include<vector>

#include<deque>

using namespace std;

const int MAX=1e5+10; 

vector<int>v[MAX];

deque<int>q;

int d[MAX];

int vis[MAX];

void bfs()

{

	while(q.size())

	{

		

		int x=q.front();

		q.pop_front();

		for(int i=0;i<v[x].size();i++)

		{

			int y=v[x][i];

			if(!vis[y])

			{

				if(d[y]==999999999)

				d[y]=x;

				q.push_back(y);

				vis[y]=1;

			}

		}

	}

}

int main()

{

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=m;i++)

	{

		int hhn,hn;

		scanf("%d%d",&hhn,&hn);

		v[hhn].push_back(hn);

		v[hn].push_back(hhn);

	} 

	d[1]=0;

	vis[1]=1;

	for(int i=2;i<=n;i++)

	{

		d[i]=999999999;vis[i]=0;

	}

	q.push_back(1);

	bfs();

	int flag=1;

	for(int i=2;i<=n;i++)

	{

		if(d[i]==999999999)

		{

			flag=0;break;

		}

	}

	if(flag)

	{

		cout<<"Yes"<<endl;

		for(int i=2;i<=n;i++)

		cout<<d[i]<<endl;

	}else

	cout<<"No"<<endl;

return 0;

 } 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <queue>

#include <algorithm>

#include <bits/stdc++.h>





using namespace std;

using ll = long long ;



vector<int> to[100005];

const int INF = 1001001001;



int main() {



    int n,m ; 

    cin >> n  >> m  ;



    for(int i=0;i<m;++i){

      int a,b ;

      cin >> a >> b ;

      --a; --b;

      to[a].push_back(b);

      to[b].push_back(a);

    }



    queue<int> q;

    vector<int> dist(n,INF);

    vector<int> pre(n,-1);

    dist[0] = 0;

    q.push(0);

    while (!q.empty())

    {

      int v = q.front();q.pop();

      for(int u : to[v]){

        if(dist[u]!=INF) continue ;

        dist[u] = dist[v]+1;

        pre[u] = v;

        q.push(u);

      }

    }

    

    cout << "Yes" << endl;

    for(int i=0;i<n;i++){

      if(i==0) continue;

      int ans = pre[i] ;

      ++ans ;

      cout << ans << endl ;

    }

    return 0 ;

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <queue>

#include <algorithm>

#include <bits/stdc++.h>





using namespace std;

using ll = long long ;



const int INF = 1001001001;

using Graph = vector<vector<int>>;



int main() {



    int n,m ; 

    cin >> n  >> m  ;



    Graph G(n) ;

    for(int i=0;i<m;i++){

        int a,b ;

        cin >> a >> b;

        a--; b--;

        G[a].push_back(b);

        G[b].push_back(a);

    }

/*

input

a b

0 1

1 2

2 3

3 1



G[0] = {1}

G[1] = {0,2,3}

G[2] = {1,3}

G[3] = {2,1}



*/



vector<int> dist(n,-1);

vector<int> pre(n,-1);

//dist[v] はスタート頂点から頂点 v まで何ステップで到達できるかを表す

//pre[v] 前の部屋

queue<int> que;



dist[0] = 0 ;

pre[0] = 0 ;

que.push(0) ;



while (!que.empty())

{

    int v = que.front();

    que.pop();



    for(int u : G[v]){

        if(dist[u]!=-1) continue;

        dist[u] = dist[v] + 1 ;

        pre[u] = v ;

        que.push(u);

    }

}



  cout << "Yes" << endl ;

  for(int i=0;i<n;i++){

     //cout << dist[i] << endl ;

     if(i==0) continue;

     cout << ++pre[i] << endl ;

  }



    return 0 ;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



const int N = 2e5 + 5;



struct point {

    int x, y, id;

} a[N];



bool cmp1(const point& a, const point& b) {

    if (a.x != b.x) {

        return a.x < b.x;

    }

    return a.y < b.y;

}



bool cmp2(const point& a, const point& b) {

    if (a.y != b.y) {

        return a.y < b.y;

    }

    return a.x < b.x;

}



int n;

bool visited[N];

vector<pair<long long, int> > g[N];



long long prim(int x) {

    priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > pq;

    long long ans = 0;

    pq.push({0, x});

    while(!pq.empty()) {

        pair<long long, int> p = pq.top();

        pq.pop();

        x = p.second;

        if (visited[x]) {

        	continue;

		}

        ans += p.first;

        visited[x] = true;

        for(int i = 0; i < g[x].size(); i++) {

            int y = g[x][i].second;

            if (!visited[y]) {

            	pq.push(g[x][i]);

            }

        }

    }

    return ans;

}



int main() {

	ios::sync_with_stdio(false);

	cin.tie(nullptr);

	cin >> n;

	for (int i = 0; i < n; i++) {

		cin >> a[i].x >> a[i].y;

		a[i].id = i + 1;

	}

	sort(a, a + n, cmp1);

	for (int i = 1; i < n; i++) {

		g[a[i].id].push_back({abs(a[i].x - a[i - 1].x), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].x - a[i - 1].x), a[i].id});

	}

	sort(a, a + n, cmp2);

	for (int i = 1; i < n; i++) {

		g[a[i].id].push_back({abs(a[i].y - a[i - 1].y), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].y - a[i - 1].y), a[i].id});

	}

	cout << prim(1) << '\n';

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

    for(int i=1;i<=n;i++){

        scanf("%d%d", &x[i], &y[i]);

        p[i] = pii(x[i], i);

        r[i] = i;

    }

    sort(p+1, p+1+n);

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

    sort(p+1, p+1+n);

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

    printf("%d\n", ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;

using ll = long long;



const int N = 2e5 + 5;



struct point {

    int x, y, id;

} a[N];



bool cmp1(const point& a, const point& b) {

    if (a.x != b.x) {

        return a.x < b.x;

    }

    return a.y < b.y;

}



bool cmp2(const point& a, const point& b) {

    if (a.y != b.y) {

        return a.y < b.y;

    }

    return a.x < b.x;

}



int n;

bool visited[N];

vector<pair<ll, int> > g[N];



ll prim(int x) {

    priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > pq;

    ll ans = 0;

    pq.push({0, x});

    while(!pq.empty()) {

        pair<ll, int> p = pq.top();

        pq.pop();

        x = p.second;

        if (visited[x]) {

        	continue;

		}

        ans += p.first;

        visited[x] = true;

        for(int i = 0; i < g[x].size(); i++) {

            int y = g[x][i].second;

            if (!visited[y]) {

            	pq.push(g[x][i]);

            }

        }

    }

    return ans;

}



int main() {

	ios::sync_with_stdio(false);

	cin.tie(nullptr);

	cin >> n;

	for (int i = 0; i < n; i++) {

		cin >> a[i].x >> a[i].y;

		a[i].id = i + 1;

	}

	sort(a, a + n, cmp1);

	for (int i = 1; i < n; i++) {

		g[a[i].id].push_back({abs(a[i].x - a[i - 1].x), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].x - a[i - 1].x), a[i].id});

	}

	sort(a, a + n, cmp2);

	for (int i = 1; i < n; i++) {

		g[a[i].id].push_back({abs(a[i].y - a[i - 1].y), a[i - 1].id});

		g[a[i - 1].id].push_back({abs(a[i].y - a[i - 1].y), a[i].id});

	}

	cout << prim(1) << '\n';

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

    for(int i=1;i<=n;i++){

        scanf("%d%d", &x[i], &y[i]);

        p[i] = pii(x[i], i);

        r[i] = i;

    }

    sort(p+1, p+1+n);

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

    sort(p+1, p+1+n);

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

    printf("%d\n", ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define MAX 300000

#define int long long

struct Pair{

	int x,y, id;

};

int n;

Pair a[MAX+1];

typedef Pair PP;

typedef pair<int,int> pp;

vector<pair<int,int> > G[MAX+1];

bool cmp(PP a,PP b){return a.x<b.x;};

bool CMP(PP a,PP b){return a.y<b.y;};

bool visited[MAX+1];

int ans;

#define fi first

#define se second

void DJK(int x){

	ans = 0;

	priority_queue<pp,vector<pp> ,greater<pp> >pq;

	pq.push({0,x});

	while(!pq.empty()){

		int u = pq.top().se;

		int w = pq.top().fi;

		pq.pop();

		if(visited[u]) continue;

		ans+=w;

		visited[u] = 1;

		for(pp ii : G[u]){

			int v = ii.se;

			pq.push({ii.fi,v});

		}

	}

}

signed main(){

	cin >> n;

	for(int i=1;i<=n;i++){

		cin >> a[i].x >> a[i].y;

		a[i].id = i;

	}

	sort(a+1,a+n+1,cmp);

	for(int i=2;i<=n;i++){

		G[a[i].id].push_back({a[i].x-a[i-1].x,a[i-1].id});

		G[a[i-1].id].push_back({a[i].x-a[i-1].x,a[i].id});

	}

	sort(a+1,a+n+1,CMP);

	for(int i=2;i<=n;i++){

		G[a[i].id].push_back({a[i].y-a[i-1].y,a[i-1].id});

		G[a[i-1].id].push_back({a[i].y-a[i-1].y,a[i].id});

	}

	DJK(1);

	cout << ans;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int, pii> pipii;

typedef pair<pii, pii> piipii;



#define mp make_pair

#define fi first

#define se second

#define all(a) (a).begin(), (a).end()

#define sz(a) (int)(a).size()

#define eb emplace_back

pii p[100005];

int x[100005], y[100005];

int r[100005];

int root(int a){

    return r[a] == a?a:(r[a]=root(r[a]));

}

void merge(int a, int b){

    a = root(a), b = root(b);

    if(a != b) r[a] = b;

}

int main(){

    int n;

    scanf("%d", &n);   

    for(int i=1;i<=n;i++){

        scanf("%d%d", &x[i], &y[i]);

        p[i] = pii(x[i], i);

        r[i] = i;

    }

    sort(p+1, p+1+n);

    vector<pipii> edges;

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    for(int i=1;i<=n;i++) p[i] = pii(y[i], i);

    sort(p+1, p+1+n);

    for(int i=2;i<=n;i++) edges.eb(pipii(p[i].fi-p[i-1].fi, pii(p[i].se, p[i-1].se)));

    sort(all(edges));

    int ans = 0;

    for(int i=0;i<sz(edges);i++){

        if(root(edges[i].se.fi) != root(edges[i].se.se)){

            merge(edges[i].se.fi, edges[i].se.se);

            ans += edges[i].fi;

        }

    }

    printf("%d\n", ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



struct point

{

	int x, y, id;

} p[100005];



vector< pair<int, pair<int, int> > > q;



bool cmpx(point a, point b)

{

	if (a.x != b.x) return a.x > b.x;

	else return a.y > b.y;

}



bool cmpy(point a, point b)

{

	if (a.y != b.y) return a.y > b.y;

	else return a.x > b.x;

}



int up[100005];



void makesets()

{

	for (int i = 0; i < 100001; i++)

	{

		up[i] = -1;

	}

}



int Find(int x)

{

	if (up[x] < 0) return x;

	int root = Find(up[x]);

	up[x] = root;

	return root;

}



void Union(int x, int y)

{

	if (Find(x) == Find(y)) return;

	if (up[x] < up[y])

	{

		up[x] += up[y];

		up[y] = x;

	}

	else

	{

		up[y] += up[x];

		up[x] = y;

	}

}



int n;



signed main()

{

	scanf("%d", &n);

	for (int i = 1; i <= n; i++)

	{

		scanf("%d%d", &p[i].x, &p[i].y);

		p[i].id = i;

	}

	sort(p+1, p+1+n, cmpx);

	for (int i = 1; i < n; i++)

	{

		q.push_back(make_pair(p[i].x - p[i+1].x, make_pair(p[i].id, p[i+1].id)));

	}

	sort(p+1, p+1+n, cmpy);

	for (int i = 1; i < n; i++)

	{

		q.push_back(make_pair(p[i].y - p[i+1].y, make_pair(p[i].id, p[i+1].id)));

	}

	sort(q.begin(), q.end());

	makesets();

	long long res = 0;

	for (int i = 0; i < q.size(); i++)

	{

		int u = q[i].second.first, v = q[i].second.second;

		if (Find(u) != Find(v))

		{

			Union(Find(u), Find(v));

			res += q[i].first;

		}

	}

	printf("%lld", res);

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define mp make_pair

#define pb push_back

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef vector<int > vi;

typedef pair<int ,int > pii;

typedef vector<pii> vii;

const int inf=0x3f3f3f3f, maxn=100007, mod=1e9+7;

const ll linf=0x3f3f3f3f3f3f3f3fLL;

const ll P=19260817;

int fa[100007];

int n;

inline int find(int x){

	return x==fa[x]? x: fa[x]=find(fa[x]);

}

struct node{

	int x,y,id;

}a[100007];

inline bool cmp1(const node& a,const node& b){

	return a.x<b.x;

}

inline bool cmp2(const node& a,const node& b){

	return a.y<b.y;

}

struct edge{

	int x,y,val;

}e[100005<<2];

inline bool cmp3(const edge& a,const edge &b){

	return a.val<b.val;

}

int cnt;

int main(){

	scanf("%d",&n);

	for(int i=1;i<=n;i++)fa[i]=i;

	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;

	sort(a+1,a+n+1,cmp1);

	for(int i=1;i<n;i++){

		e[++cnt].x=a[i].id;

		e[cnt].y=a[i+1].id;

		e[cnt].val=abs(a[i].x-a[i+1].x);

	}

	sort(a+1,a+n+1,cmp2);

	for(int i=1;i<n;i++){

		e[++cnt].x=a[i].id;

		e[cnt].y=a[i+1].id;

		e[cnt].val=abs(a[i].y-a[i+1].y);

	}

	sort(e+1,e+cnt+1,cmp3);

	int p=0;

	ll ans=0;

	for(int i=1;i<=cnt;i++){

		int fx=find(e[i].x);

		int fy=find(e[i].y);

		if(fx==fy)continue;

		fa[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)break;

	}

	printf("%lld",ans);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define int long long

vector<pair<int,pair<int,int> > >points;

vector<pair<int,pair<int,int> > > edge;

int par[100005];

bool cmp(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

	return a.second.first<b.second.first;

}

bool cmp2(pair<int,pair<int,int> > &a,pair<int,pair<int,int> > &b ){

	return a.second.second<b.second.second;

}

void init(){

	for(int i=1;i<=100000;i++){

		par[i]=i;

	}

}

int find(int i){

	if(par[i]==i) return par[i];

	return par[i]=find(par[i]);

}

void dsu(int i,int j){

	par[find(i)]=find(j);

}

bool same(int i,int j){

	return find(i)==find(j);

}

signed main(){

//	freopen("plantree.inp","w",stdin);

//	freopen("plantree.out","r",stdout);

	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	int n;

	cin>>n;

	for(int i=1;i<=n;i++){

		int x,y;

		cin>>x>>y;

		points.push_back(make_pair(i,make_pair(x,y)));

	}	

	init();

	sort(points.begin(),points.end(),cmp);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.first-points[i-1].second.first,make_pair(points[i].first,points[i-1].first)));

	}

	sort(points.begin(),points.end(),cmp2);

	for(int i=1;i<points.size();i++){

		edge.push_back(make_pair(points[i].second.second-points[i-1].second.second,make_pair(points[i].first,points[i-1].first)));

	}

	sort(edge.begin(),edge.end());

	int ans=0;

	

	for(int i=0;i<edge.size();i++){

		if(!same(edge[i].second.second,edge[i].second.first)){

			ans+=edge[i].first;

			dsu(edge[i].second.second,edge[i].second.first);

		}

	}

	cout<<ans;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define mp make_pair

#define pb push_back

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

typedef vector<int > vi;

typedef pair<int ,int > pii;

typedef vector<pii> vii;

const int inf=0x3f3f3f3f, maxn=100007, mod=1e9+7;

const ll linf=0x3f3f3f3f3f3f3f3fLL;

const ll P=19260817;

int fa[100007];

int n;

inline int find(int x){

	return x==fa[x]? x: fa[x]=find(fa[x]);

}

struct node{

	int x,y,id;

}a[100007];

inline bool cmp1(const node& a,const node& b){

	return a.x<b.x;

}

inline bool cmp2(const node& a,const node& b){

	return a.y<b.y;

}

struct edge{

	int x,y,val;

}e[100005<<2];

inline bool cmp3(const edge& a,const edge &b){

	return a.val<b.val;

}

int cnt;

int main(){

	scanf("%d",&n);

	for(int i=1;i<=n;i++)fa[i]=i;

	for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),a[i].id=i;

	sort(a+1,a+n+1,cmp1);

	for(int i=1;i<n;i++){

		e[++cnt].x=a[i].id;

		e[cnt].y=a[i+1].id;

		e[cnt].val=abs(a[i].x-a[i+1].x);

	}

	sort(a+1,a+n+1,cmp2);

	for(int i=1;i<n;i++){

		e[++cnt].x=a[i].id;

		e[cnt].y=a[i+1].id;

		e[cnt].val=abs(a[i].y-a[i+1].y);

	}

	sort(e+1,e+cnt+1,cmp3);

	int p=0;

	ll ans=0;

	for(int i=1;i<=cnt;i++){

		int fx=find(e[i].x);

		int fy=find(e[i].y);

		if(fx==fy)continue;

		fa[fx]=fy;

		ans+=e[i].val;

		p++;

		if(p==n-1)break;

	}

	printf("%lld",ans);

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>



using namespace std;

const int maxN=1e5+5;



int fa[maxN];



struct node

{

    int x,y,id;

}a[maxN],b[maxN];



struct edge

{

    int f,t,w;

}e[maxN*2];



bool cmp1(node x1,node x2)

{

    return x1.x<x2.x;

}



bool cmp2(node x1,node x2)

{

    return x1.y<x2.y;

}



bool cmp3(edge x1,edge x2)

{

    return x1.w<x2.w;

}



int find(int x)

{

    if(fa[x]==-1)return x;

    return fa[x]=find(fa[x]);

}



int uni(int f,int t)

{

    f=find(f);t=find(t);

    if(f==t)return 0;

    fa[f]=t;return 1;

}



int main()

{

    int k,n,cnt=0,now=0;

    long long ans=0;

    memset(fa,-1,sizeof(fa));

    scanf("%d",&n);

    for(k=0;k<n;k++)

    {

        scanf("%d%d",&a[k].x,&a[k].y);

        a[k].id=k;b[k].id=k;

        b[k].x=a[k].x;

        b[k].y=a[k].y;

    }

    sort(a,a+n,cmp1);

    sort(b,b+n,cmp2);

    for(k=1;k<n;k++)

    {

        e[cnt].f=a[k-1].id;

        e[cnt].t=a[k].id;

        e[cnt].w=a[k].x-a[k-1].x;

        cnt++;

    }

    for(k=1;k<n;k++)

    {

        e[cnt].f=b[k-1].id;

        e[cnt].t=b[k].id;

        e[cnt].w=b[k].y-b[k-1].y;

        cnt++;

    }

    sort(e,e+cnt,cmp3);

    for(k=0;k<cnt;k++)

    {

        if(uni(e[k].f,e[k].t))

        {

            ans+=e[k].w;

            now++;

        }

        if(now==n-1)break;

    }

    cout<<ans;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

struct EDGE

{

	int x,y,data;

}edge[2*100000+10];

struct node

{

	int x,y,id;

}arr[100000+10];

int fa[100500];

int find(int x)

{

	if(x==fa[x]) return fa[x];

	return fa[x] = find(fa[x]);

}

bool cmp1(node a,node b)

{

	return a.x<b.x;

}

bool cmp2(node a,node b)

{

	return a.y<b.y;

}

bool cmp(EDGE a,EDGE b)

{

	return a.data<b.data;

}

int cnt = 0;

void built(int x,int y,int data)

{

	edge[++cnt].x=x;

	edge[cnt].y=y;

	edge[cnt].data=data;

}

int main()

{

	int n;

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		fa[i]=i;

		scanf("%d%d",&arr[i].x,&arr[i].y);

		arr[i].id = i;

	}

	sort(arr+1,arr+1+n,cmp1);

	for(int i=2;i<=n;i++)

	built(arr[i-1].id,arr[i].id,arr[i].x-arr[i-1].x);

	sort(arr+1,arr+1+n,cmp2);

	for(int i=2;i<=n;i++)

	built(arr[i-1].id,arr[i].id,arr[i].y-arr[i-1].y);

	long long res = 0;

	sort(edge+1,edge+1+cnt,cmp);

	for(int i=1;i<=cnt;i++)

	if(find(edge[i].x)!=find(edge[i].y))

	{

		res += edge[i].data;

		fa[fa[edge[i].x]] = fa[edge[i].y];

	}

	printf("%d\n",res);

	return 0;

}


// Fastest known solution for this problem: