Submission #1855317


Source Code Expand

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define INF 1e18

typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef vector < pair<int, int> > vii;
typedef long double ld;
typedef tree<pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

ll n, x, y, ans, par[111111];
vector <pair<ii, int> > town;
priority_queue <pair<ll, ii>, vector<pair<ll, ii> >, greater<pair<ll, ii> > > edges;

ll root(ll a){
	if(par[a]<0){
		return a;
	}
	else{
		par[a]=root(par[a]);
		return par[a];
	}
}

void merge(ll a, ll b){
	a=root(a);
	b=root(b);
	if(a==b){
		return;
	}
	else{
		if(par[a]>par[b]){
			swap(a,b);
		}
		par[a]+=par[b];
		par[b]=a;
	}
}

bool cmp(pair<ii, int>  a, pair<ii, int>  b){
	return a.fi.fi<b.fi.fi;
}
bool cmp1(pair<ii, int>  a, pair<ii, int>  b){
	return a.fi.se<b.fi.se;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> x >> y;
		town.pb(mp(mp(x,y),i));
	}
	sort(town.begin(),town.end(),cmp);
	for(int i = 1; i < n; i++){
		edges.push(mp(town[i].fi.fi-town[i-1].fi.fi,mp(town[i-1].se,town[i].se)));
	}
	sort(town.begin(),town.end(),cmp1);
	for(int i = 1; i < n; i++){
		edges.push(mp(town[i].fi.se-town[i-1].fi.se,mp(town[i-1].se,town[i].se)));
	}
	for(int i = 0; i < n; i++){
		par[i]=-1;
	}
	while(!edges.empty()){
		auto cur = edges.top();
		edges.pop();
		if(root(cur.se.fi)!=root(cur.se.se)){
			ans+=cur.fi;
			merge(cur.se.fi,cur.se.se);
		}
	}
	cout << ans;
	return 0;
}

Submission Info

Submission Time
Task D - Built?
User vjudge3
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1870 Byte
Status AC
Exec Time 96 ms
Memory 10864 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 28
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 95 ms 10224 KB
02.txt AC 94 ms 10608 KB
03.txt AC 94 ms 10480 KB
04.txt AC 94 ms 10224 KB
05.txt AC 94 ms 10736 KB
06.txt AC 94 ms 10864 KB
07.txt AC 95 ms 9584 KB
08.txt AC 94 ms 10608 KB
09.txt AC 96 ms 10096 KB
10.txt AC 94 ms 10608 KB
11.txt AC 95 ms 9072 KB
12.txt AC 95 ms 9072 KB
13.txt AC 83 ms 9328 KB
14.txt AC 83 ms 9456 KB
15.txt AC 81 ms 10864 KB
16.txt AC 82 ms 10096 KB
17.txt AC 85 ms 9200 KB
18.txt AC 85 ms 9584 KB
19.txt AC 82 ms 10352 KB
20.txt AC 81 ms 10224 KB
21.txt AC 82 ms 10864 KB
22.txt AC 82 ms 10480 KB
23.txt AC 57 ms 9072 KB
24.txt AC 82 ms 10096 KB
25.txt AC 1 ms 256 KB
26.txt AC 1 ms 256 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB