Submission #4025574
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
const int inf = 1e9;
const ll linf = 1e18;
const ll mod = 1e9 + 7;
int par[100000];
int find(int x)
{
if(par[x] == x) return x;
return par[x] = find(par[x]);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if(x == y) return;
par[x] = y;
}
bool same(int x, int y)
{
return find(x) == find(y);
}
ll cost(int a, int b, int c, int d)
{
return min(abs(a-c), abs(b-d));
}
int main()
{
int n;
cin >> n;
vector<tuple<int, int, int> > xy, yx;
rep(i, n){
int x, y;
cin >> x >> y;
xy.pb(mt(x, y, i));
yx.pb(mt(y, x, i));
}
sort(all(xy));
sort(all(yx));
vector<tuple<ll, int, int> > es;
rep(i, n-1) es.pb(mt(cost(get<0>(xy[i]), get<1>(xy[i]), get<0>(xy[i+1]), get<1>(xy[i+1])), get<2>(xy[i]), get<2>(xy[i+1])));
rep(i, n-1) es.pb(mt(cost(get<0>(yx[i]), get<1>(yx[i]), get<0>(yx[i+1]), get<1>(yx[i+1])), get<2>(yx[i]), get<2>(yx[i+1])));
sort(all(es));
rep(i, n) par[i] = i;
ll ans = 0;
for(auto e : es){
int x, y, c;
tie(c, x, y) = e;
if(!same(x, y)){
ans += c;
unite(x, y);
}
}
cout << ans << endl;
return 0;
}
Submission Info
Submission Time |
|
Task |
D - Built? |
User |
yuto953 |
Language |
C++14 (GCC 5.4.1) |
Score |
500 |
Code Size |
1600 Byte |
Status |
AC |
Exec Time |
126 ms |
Memory |
9640 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 |
124 ms |
9640 KB |
02.txt |
AC |
126 ms |
8104 KB |
03.txt |
AC |
125 ms |
8872 KB |
04.txt |
AC |
124 ms |
7720 KB |
05.txt |
AC |
126 ms |
7848 KB |
06.txt |
AC |
126 ms |
9128 KB |
07.txt |
AC |
125 ms |
9640 KB |
08.txt |
AC |
125 ms |
9256 KB |
09.txt |
AC |
121 ms |
9384 KB |
10.txt |
AC |
121 ms |
9256 KB |
11.txt |
AC |
121 ms |
7976 KB |
12.txt |
AC |
122 ms |
9512 KB |
13.txt |
AC |
101 ms |
7976 KB |
14.txt |
AC |
101 ms |
9512 KB |
15.txt |
AC |
98 ms |
9512 KB |
16.txt |
AC |
113 ms |
9128 KB |
17.txt |
AC |
120 ms |
9000 KB |
18.txt |
AC |
116 ms |
8360 KB |
19.txt |
AC |
97 ms |
8488 KB |
20.txt |
AC |
98 ms |
8872 KB |
21.txt |
AC |
98 ms |
8360 KB |
22.txt |
AC |
102 ms |
7848 KB |
23.txt |
AC |
73 ms |
9128 KB |
24.txt |
AC |
95 ms |
9256 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 |