stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

 

#define pb push_back

#define mp make_pair

#define f0(i,b) for(int i=0;i<(b);i++)

#define f1(i,b) for(int i=1;i<=(b);i++)

#define f2(i,a,b) for(int i=(a);i<=(b);i++)

#define sf(a) scanf("%d",&a);

#define sfl(a) scanf("%lld",&a);

 

bool vis[200005];

vector<int> v[200001];

int l[200001],p[200001];

 

void edge(int a, int b)

{

    v[a].pb(b);

    v[b].pb(a);

}

void bfs(int u)

{

    queue<int> q;

    q.push(u);

    vis[u] = true;

    int x;

    while (!q.empty()) {

 

        int f = q.front();

        q.pop();

 

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

            x = v[f][i];

            if(!vis[x]){

                q.push(x);

                l[x] = l[f] + 1;

                p[x] = f;

                vis[x] = true;

            }

        }

    }

 

}

void init(){

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

  l[i] = 0;

 

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

     v[i].clear();

 

}

int main(){

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

    //freopen("input.txt", "r", stdin);

    //freopen("output.txt", "w", stdout);

     int node, edg, a, b;

     cin >> node >> edg;

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

         cin >> a >> b;

         edge(a,b);

     }

     l[1] = 1;

     bfs(1);

     int c = 0;

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

       if(vis[i])

          c++;

     }

     if(c == node){

      cout << "Yes" << endl;

      for(int i = 2 ; i <= node ; i++) cout << p[i] << endl;

     }

     else cout << "No" << endl;

 

 

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4257597499767298x):
#include <bits/stdc++.h>

using namespace std;

#define INF 100005



int main()

{

	//freopen("input.txt","r",stdin);

    //freopen("output.txt","w",stdout);

    //ios_base::sync_with_stdio(false);

    //cin.tie(NULL);

	int n,m;

	cin >> n >> m;

	vector<int>adj[INF];

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

	{

		int aa,bb;

		cin >> aa >> bb;

		adj[aa].push_back(bb);

		adj[bb].push_back(aa);

	}

	int level[INF]={0};

	bool vis[INF]={false};

	int parent[INF]={0};

	queue<int>q;

	q.push(1);

	level[1]=0;

	vis[1]=true;

	while(!q.empty())

	{

		int p=q.front();

		q.pop();

		for(int i=0;i<adj[p].size();i++)

		{

			if(vis[adj[p][i]]==false)

			{

				level[adj[p][i]]=level[p]+1;

				q.push(adj[p][i]);

				vis[adj[p][i]]=true;

				parent[adj[p][i]]=p;

			}

		}

	}

	int check=0;

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

	{

		if(level[i]==0)

		{

			check=1;

			break;

		}

	}

	if(check==1)

	{

		cout << "No" << "\n";

		return 0;

	}

	cout << "Yes" << "\n";

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

	{

		cout << parent[i] << "\n";

	}

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

vector<int> adj[100001];

bool visited[100001];

int ans[100001];

 

void bfs(){

    queue<int> q;

    q.push(1);

    visited[1] = true;

    

    while(q.size()){

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

        for(auto i : adj[cur]){

            if(!visited[i]){

                ans[i] = cur;

                q.push(i);

                visited[i] = true;

            }

        }

    }

}

int main()

{

	memset(visited,false,sizeof(visited));

	memset(ans,0,sizeof(ans));

	int nodes,edges;

	cin >> nodes >> edges;

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

	{

		int u,v;

		cin >> u >> v;

		adj[u].push_back(v);

		adj[v].push_back(u);

	}

	bfs();

	cout << "Yes\n";

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

		cout << ans[i] << "\n";

}

// Faster C++ solution for the same problem (measured speedup: 1.1340379047698563x):
#include<bits/stdc++.h>

#define endl "\n" 

using namespace std;

#define debug(x) cout<<"The value of "<<#x<<" is "<<x<<endl;

typedef long long ll;

typedef unsigned long long ull;

bool visited[100003];

vector<int>adj[100003];

int parent[100003];

void bfs(){

    visited[1]=true;

    queue<int>qa;

    qa.push(1);

    while(qa.size()){

        int a=qa.front();

        qa.pop();

        for(auto i:adj[a]){

            if(!visited[i]){

                parent[i]=a;

                visited[i]=1;

                qa.push(i);

            }

        }

    }

}

int main()

{

    cin.sync_with_stdio(0); cin.tie(0); 

    int n,m;

    cin>>n>>m;

    while(m--){

        int a,b;

        cin>>a>>b;

        adj[a].push_back(b);

        adj[b].push_back(a);

    }

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

    parent[i]=0,visited[i]=0;

    bfs();

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

    if(!visited[i]){

        cout<<"No"<<endl;

        return 0;

    }

    cout<<"Yes"<<endl;

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

    cout<<parent[i]<<endl;

}
// Slower C++ solution:
#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;

	

}

// Faster C++ solution for the same problem (measured speedup: 2.4850851662409394x):
#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;

 }
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll inf = 1e9;

#define sz              300005

#define Fast            ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);



int par[sz];

bool vis[sz];

vector < int > V[sz];



void bfs(int src){



    memset(vis,0,sizeof(vis));

    memset(par,0,sizeof(par));

    queue< int > q;

    q.push(1);

    vis[1]=0;



    while(!q.empty()){

        int u=q.front();

        q.pop();



        for(int i=0; i<V[u].size(); i++){

            int v=V[u][i];

            if(vis[v]==0){

                par[v]=u;

                vis[v]=1;

                q.push(v);

            }

        }

    }

}



int main(){

    Fast;

    int a,b,c, i,j, n,m,t, cs=1;



    cin>>n>>m;

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

        cin>>a>>b;

        V[a].push_back(b);

        V[b].push_back(a);

    }

    bfs(1);



    bool flag=true;

    vector < int > pp;

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

        if(par[i]==0){

            flag=false;break;

        }

        pp.push_back(par[i]);

    }

    if(!flag){

        cout<<"No"<<endl;

    }

    else{

        cout<<"Yes"<<endl;

        for(i=0; i<n-1; i++){

            cout<<pp[i]<<endl;

        }

    }





    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 3.155798704367647x):
#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;

 }
// Slower C++ solution:
#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 ;

}

// Faster C++ solution for the same problem (measured speedup: 3.49347655723377x):
#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 ;

}
// Slower C++ solution:
#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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


// Faster C++ solution for the same problem (measured speedup: 1.4093043883219716x):
#include<bits/stdc++.h>



using namespace std;



typedef pair<int, int> ii;





vector<ii> pointsX;

vector<ii> pointsY;



vector<pair<int, ii>> edges;

int parents[1000002];



int find(int x){

    if(parents[x] == x){

        return x;

    }

    parents[x] = find(parents[x]);

    return parents[x];

}



int main(){

//    freopen("A.inp", "r", stdin);



    int n;

    cin >> n;



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

        parents[i] = i;

        int a, b;

        cin >> a >> b;

        pointsX.push_back(ii(a, i));

        pointsY.push_back(ii(b, i));

    }



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

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



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

        int w = abs(pointsX[i].first - pointsX[i + 1].first);

        ii e = ii(pointsX[i].second, pointsX[i + 1].second);

        edges.push_back(pair<int, ii>(w, e));

        w = abs(pointsY[i].first - pointsY[i + 1].first);

        e = ii(pointsY[i].second, pointsY[i + 1].second);

        edges.push_back(pair<int, ii>(w, e));

    }



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

    int use = 0;

    int ans = 0;

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

        int st = find(edges[i].second.first);

        int en = find(edges[i].second.second);



        if(st != en){

            ans += edges[i].first;

            parents[st] = en;

            use++;

        }

        if(use == n - 1){

            cout << ans;

            return 0;

        }



    }



    cout << ans;



    return 0;



}
// Slower C++ solution:
#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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


// Faster C++ solution for the same problem (measured speedup: 1.3920795325537794x):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back

#define mp make_pair

#define fi first

#define se second

int n,pset[100007];

long long x[100007],y[100007],ans=0;

pair < pair < long long,long long > , int > p[100007];

vector < pair < long long, pair < int,int > > > cur;

int fset(int x){

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

    return pset[x]=fset(pset[x]);

}

int main(){

    ios_base::sync_with_stdio(0);

    cin>>n;

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

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

        p[i]=mp(mp(x[i],y[i]),i);

        pset[i]=i;

    }

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

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

        cur.pb(mp(p[i].fi.fi-p[i-1].fi.fi,mp(p[i].se,p[i-1].se)));

    }

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

        p[i]=mp(mp(y[i],x[i]),i);

    }

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

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

        cur.pb(mp(p[i].fi.fi-p[i-1].fi.fi,mp(p[i].se,p[i-1].se)));

    }

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

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

        int a=cur[i].se.fi,b=cur[i].se.se;

        long long val=cur[i].fi;

        a=fset(a);

        b=fset(b);

        if(a!=b){

            pset[b]=a;

            ans+=val;

        }

    }

    cout<<ans;

}
// Slower C++ solution:
#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];



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;

	}

}



signed main() {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	int n; cin >> n;

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

		cin >> 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());

	for (int i=0; i<100001; i++) up[i] = -1;

	long long ans = 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));

			ans += q[i].first;

		}

	}

	cout << ans;

}


// Faster C++ solution for the same problem (measured speedup: 1.410620207609745x):
#include<bits/stdc++.h>

using namespace std;

#define mod 1000000007

#define MOD(x) ((x)%mod)

int n;

map<pair<int,int>,bool>m;

struct point

{

	long long x,y;

	int id;

}p[100005];

int cnt;

struct edge

{

	int u,v;

	long long w;

}e[400005];

bool cmp1(point a,point b)

{

	return a.x<b.x;

}

bool cmp2(point a,point b)

{

	return a.y<b.y;

}

bool cmp3(edge a,edge b)

{

	return a.w<b.w;

}

int fa[100005];

int find(int x)

{

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

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

}

long long sum;

int main()

{

	scanf("%d",&n);

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

	{

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

		p[i].id=i;

	}

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

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

	{

		if(m[make_pair(p[i].id,p[i+1].id)]) continue;

		if(m[make_pair(p[i+1].id,p[i].id)]) continue;

		m[make_pair(p[i].id,p[i+1].id)]=1;

		cnt++;

		e[cnt].u=p[i].id;

		e[cnt].v=p[i+1].id;

		e[cnt].w=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

	}

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

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

	{

		if(m[make_pair(p[i].id,p[i+1].id)]) continue;

		if(m[make_pair(p[i+1].id,p[i].id)]) continue;

		m[make_pair(p[i].id,p[i+1].id)]=1;

		cnt++;

		e[cnt].u=p[i].id;

		e[cnt].v=p[i+1].id;

		e[cnt].w=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

	}

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

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

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

	{

		if(find(e[i].u)==find(e[i].v)) continue;

		fa[find(e[i].u)]=find(e[i].v);

		sum+=e[i].w;

	}

	printf("%lld",sum);

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

#include<cmath>

#include<iostream>

using namespace std;

const int inf=0x3f3f3f3f;

const int maxn=1e5+10;



struct edge{

    int u,v,w;

    edge(int _u=0,int _v=0,int _w=0):u(_u),v(_v),w(_w){

    }

    bool operator<(const edge & a)const{

        return w<a.w;

    }

}es[2*maxn];



struct point{

    int x,y,id;

    point(int _x=0,int _y=0,int _id=0):x(_x),y(_y),id(_id){}

} ps[maxn];



int n,cnt,f[maxn];



bool cmpx(point a,point b){

    return a.x<b.x;

}



bool cmpy(point a,point b){

    return a.y<b.y;

}



int findf(int a){

    return ((f[a]==-1)?a:f[a]=findf(f[a]));

}



int kruskal(){

    int i,ans=0,cc=0;

    memset(f,0xff,sizeof(f));

    sort(es,es+cnt);

    for(i=0;i<cnt;i++){

        int u=es[i].u,v=es[i].v,w=es[i].w;

        int fu=findf(u),fv=findf(v);

        if(fu!=fv){

            ans+=w,++cc;

            f[fu]=fv;

        }

        if(cc==n-1)break;

    }

    if(cc<n-1)return -1;

    return ans;

}

int main(){

    int i,j,k,c,un,vn;

    scanf("%d",&n);

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

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

        ps[i].id=i;

    }

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

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

        es[cnt++]=edge(ps[i].id,ps[i+1].id,(ps[i+1].x-ps[i].x));

    }

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

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

        es[cnt++]=edge(ps[i].id,ps[i+1].id,(ps[i+1].y-ps[i].y));

    }

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

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.024046376309983x):
#include<bits/stdc++.h>

using namespace std;

#define mod 1000000007

#define MOD(x) ((x)%mod)

int n;

map<pair<int,int>,bool>m;

struct point

{

	long long x,y;

	int id;

}p[100005];

int cnt;

struct edge

{

	int u,v;

	long long w;

}e[400005];

bool cmp1(point a,point b)

{

	return a.x<b.x;

}

bool cmp2(point a,point b)

{

	return a.y<b.y;

}

bool cmp3(edge a,edge b)

{

	return a.w<b.w;

}

int fa[100005];

int find(int x)

{

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

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

}

long long sum;

int main()

{

	scanf("%d",&n);

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

	{

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

		p[i].id=i;

	}

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

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

	{

		if(m[make_pair(p[i].id,p[i+1].id)]) continue;

		if(m[make_pair(p[i+1].id,p[i].id)]) continue;

		m[make_pair(p[i].id,p[i+1].id)]=1;

		cnt++;

		e[cnt].u=p[i].id;

		e[cnt].v=p[i+1].id;

		e[cnt].w=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

	}

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

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

	{

		if(m[make_pair(p[i].id,p[i+1].id)]) continue;

		if(m[make_pair(p[i+1].id,p[i].id)]) continue;

		m[make_pair(p[i].id,p[i+1].id)]=1;

		cnt++;

		e[cnt].u=p[i].id;

		e[cnt].v=p[i+1].id;

		e[cnt].w=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));

	}

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

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

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

	{

		if(find(e[i].u)==find(e[i].v)) continue;

		fa[find(e[i].u)]=find(e[i].v);

		sum+=e[i].w;

	}

	printf("%lld",sum);

	return 0;

}