Submission #3813580
Source Code Expand
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <utility>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int,int> pii;
class UnionFind {
private:
int sz;
vector<int> par, size_;
public:
UnionFind(){}
UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
rep(i,sz)par[i] = i;
}
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x,int y){
x = find(x), y = find(y);
if(x == y) return;
if(size_[x] < size_[y]) swap(x,y);
par[y] = x;
size_[x] += size_[y];
}
int size(int x){
x = find(x);
return size_[x];
}
bool same(int x,int y){
return find(x) == find(y);
}
};
struct edge{
int u,v;
int cost;
};
bool operator<(const edge&a, const edge&b){
return a.cost < b.cost;
}
int main(){
int n;
cin >> n;
vector<pii>x(n);
vector<pii>y(n);
rep(i,n){
cin >> x[i].first >> y[i].first;
x[i].second = y[i].second = i;
}
sort(x.begin(),x.end(),[&](pii a,pii b){return a.first < b.first;});
sort(y.begin(),y.end(),[&](pii a,pii b){return a.first < b.first;});
vector<edge>a;
rep(i,n-1){
a.push_back({x[i].second,x[i+1].second,x[i+1].first-x[i].first});
a.push_back({y[i].second,y[i+1].second,y[i+1].first-y[i].first});
}
sort(a.begin(),a.end());
ll ans = 0;
UnionFind uf(n);
for(auto &e:a){
if(uf.same(e.u,e.v))continue;
uf.unite(e.u,e.v);
ans += e.cost;
}
cout << ans << endl;
}
Submission Info
Submission Time |
|
Task |
D - Built? |
User |
polyomino |
Language |
C++14 (GCC 5.4.1) |
Score |
500 |
Code Size |
1783 Byte |
Status |
AC |
Exec Time |
115 ms |
Memory |
6388 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
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 |
114 ms |
4980 KB |
02.txt |
AC |
114 ms |
4980 KB |
03.txt |
AC |
114 ms |
5620 KB |
04.txt |
AC |
114 ms |
6388 KB |
05.txt |
AC |
115 ms |
4980 KB |
06.txt |
AC |
114 ms |
4980 KB |
07.txt |
AC |
114 ms |
5620 KB |
08.txt |
AC |
114 ms |
4980 KB |
09.txt |
AC |
111 ms |
4980 KB |
10.txt |
AC |
111 ms |
5876 KB |
11.txt |
AC |
110 ms |
4980 KB |
12.txt |
AC |
110 ms |
5876 KB |
13.txt |
AC |
79 ms |
5748 KB |
14.txt |
AC |
80 ms |
4980 KB |
15.txt |
AC |
79 ms |
4980 KB |
16.txt |
AC |
91 ms |
4980 KB |
17.txt |
AC |
100 ms |
6388 KB |
18.txt |
AC |
96 ms |
4980 KB |
19.txt |
AC |
79 ms |
4980 KB |
20.txt |
AC |
80 ms |
6004 KB |
21.txt |
AC |
80 ms |
6004 KB |
22.txt |
AC |
84 ms |
4980 KB |
23.txt |
AC |
45 ms |
6260 KB |
24.txt |
AC |
64 ms |
4980 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 |