Submission #2529128


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
vector<int> oya(1e5 + 10);
vector<int> rnk(1e5 + 10);
void init(int n) {
  for (int i = 0; i < n; i++) {
    oya[i] = i;
    rnk[i] = 0;
  }
}
int find(int x) {
  if (oya[x] == x) return x;
  return oya[x] = find(oya[x]);
}
void unite(int x, int y) {
  x = find(x); y = find(y);
  if (x == y) return;
  if (rnk[x] < rnk[y]) {
    oya[x] = y;
  } else {
    oya[y] = x;
    if (rnk[x] == rnk[y]) rnk[x]++;
  }
}
bool same(int x, int y) {
  return find(x) == find(y);
}
signed main() {
  cin.tie(0); ios::sync_with_stdio(false);
  struct town { int i; long long x, y; };
  int n; cin >> n;
  vector<town> t(n);
  for (int i = 0; i < n; i++) {
    t[i].i = i;
    cin >> t[i].x >> t[i].y;
  }
  struct edge { int from, to; long long cost; };
  vector<edge> e;
  sort(t.begin(), t.end(), [](auto a, auto b) { return a.x < b.x; });
  for (int i = 0; i < n - 1; i++) {
    e.push_back(edge{t[i].i, t[i + 1].i, min(abs(t[i + 1].x - t[i].x), abs(t[i + 1].y - t[i].y))});
  }
  sort(t.begin(), t.end(), [](auto a, auto b) { return a.y < b.y; });
  for (int i = 0; i < n - 1; i++) {
    e.push_back(edge{t[i].i, t[i + 1].i, min(abs(t[i + 1].x - t[i].x), abs(t[i + 1].y - t[i].y))});
  }
  sort(e.begin(), e.end(), [](auto a, auto b) { return a.cost < b.cost; });
  init(n);
  long long res = 0;
  for (auto i : e) {
    if (same(i.from, i.to)) continue;
    res += i.cost;
    unite(i.from, i.to);
  }
  cout << res << '\n';
  return 0;
}

Submission Info

Submission Time
Task D - Built?
User risujiroh
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1530 Byte
Status AC
Exec Time 59 ms
Memory 9456 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 59 ms 9456 KB
02.txt AC 58 ms 7664 KB
03.txt AC 58 ms 8176 KB
04.txt AC 58 ms 7920 KB
05.txt AC 58 ms 8048 KB
06.txt AC 58 ms 7792 KB
07.txt AC 58 ms 9328 KB
08.txt AC 58 ms 8176 KB
09.txt AC 57 ms 8176 KB
10.txt AC 56 ms 8176 KB
11.txt AC 57 ms 8432 KB
12.txt AC 57 ms 9200 KB
13.txt AC 38 ms 8944 KB
14.txt AC 37 ms 8816 KB
15.txt AC 37 ms 8176 KB
16.txt AC 39 ms 9456 KB
17.txt AC 40 ms 8176 KB
18.txt AC 40 ms 8432 KB
19.txt AC 37 ms 7920 KB
20.txt AC 39 ms 7664 KB
21.txt AC 38 ms 9456 KB
22.txt AC 40 ms 9456 KB
23.txt AC 28 ms 7664 KB
24.txt AC 37 ms 7792 KB
25.txt AC 2 ms 1024 KB
26.txt AC 2 ms 1024 KB
s1.txt AC 2 ms 1024 KB
s2.txt AC 2 ms 1024 KB