Submission #1985184


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

using ll = long long int;

const int MAX_N = (int)1e5 + 5;
const ll MOD = (ll)1e9 + 7;

struct index_point {ll index, x, y;};
struct edge {ll u, v, cost;};

int n;
vector<index_point> ips;

bool comp(const edge &e1, const edge &e2) {
  return e1.cost < e2.cost;
}

vector<edge> es;
ll vsize, esize;

/////////////////////////////////////////////////////////
ll uf_par[MAX_N];
ll uf_rank[MAX_N];

void uf_init(ll n) {
  for (ll i = 0; i < n; ++i) {
    uf_par[i] = i;
    uf_rank[i] = 0;
  }
}

ll uf_find (ll x) {
  if (uf_par[x] == x) {return x;}
  else {
    return uf_par[x] = uf_find(uf_par[x]);
  }
}

void uf_unite(ll x, ll y) {
  x = uf_find(x);
  y = uf_find(y);

  if (x == y) {return;}
  if (uf_rank[x] < uf_rank[y]) {uf_par[x] = y;}
  else {
    uf_par[y] = x;
    if (uf_rank[x] == uf_rank[y]) uf_rank[x] += 1;
  }
}

bool uf_same(ll x, ll y) {
  return uf_find(x) == uf_find(y);
}
/////////////////////////////////////////////////////////

int main(void){
  // Here your code !
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    ll x, y;
    index_point ip;
    scanf("%lld", &x);
    scanf("%lld", &y);
    ip.index = i;
    ip.x = x;
    ip.y = y;
    ips.push_back(ip);
  }

  sort(ips.begin(), ips.end(), [](const index_point a, const index_point b) {return a.x < b.x;}); // sort by x
  for (int i = 1; i < ips.size(); ++i) {
    edge e;
    e.u = ips[i - 1].index;
    e.v = ips[i].index;
    e.cost = ips[i].x - ips[i - 1].x;
    es.push_back(e);
  }

  sort(ips.begin(), ips.end(), [](const index_point a, const index_point b) {return a.y < b.y;}); // sort by x
  for (int i = 1; i < ips.size(); ++i) {
    edge e;
    e.u = ips[i - 1].index;
    e.v = ips[i].index;
    e.cost = ips[i].y - ips[i - 1].y;
    es.push_back(e);
  }

  vsize = n;
  esize = es.size();

  sort(es.begin(), es.end(), comp);
  uf_init(vsize);

  ll res = 0;
  for (int i = 0; i < esize; ++i) {
    edge e = es[i];
    if (!uf_same(e.u, e.v)) {
      uf_unite(e.u, e.v);
      res += e.cost;
    }
  }

  printf("%lld\n", res);

  return 0;
}

Submission Info

Submission Time
Task D - Built?
User david
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2195 Byte
Status AC
Exec Time 68 ms
Memory 10988 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:59:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
./Main.cpp:63:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &x);
                      ^
./Main.cpp:64:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld", &y);
                      ^

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 68 ms 10608 KB
02.txt AC 68 ms 10736 KB
03.txt AC 68 ms 9836 KB
04.txt AC 68 ms 10480 KB
05.txt AC 68 ms 9708 KB
06.txt AC 68 ms 10224 KB
07.txt AC 68 ms 10736 KB
08.txt AC 68 ms 9452 KB
09.txt AC 67 ms 10988 KB
10.txt AC 66 ms 10220 KB
11.txt AC 66 ms 10480 KB
12.txt AC 67 ms 10732 KB
13.txt AC 52 ms 9968 KB
14.txt AC 52 ms 10480 KB
15.txt AC 45 ms 10864 KB
16.txt AC 46 ms 10096 KB
17.txt AC 49 ms 10476 KB
18.txt AC 48 ms 10096 KB
19.txt AC 45 ms 9708 KB
20.txt AC 47 ms 9452 KB
21.txt AC 46 ms 10736 KB
22.txt AC 47 ms 9452 KB
23.txt AC 38 ms 10608 KB
24.txt AC 45 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