Submission #1990286


Source Code Expand

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>

#define REP(i,n) for(int i=0;i<n;++i)
#define REPR(i,n) for(int i=n;i>=0;--i)
#define REPI(itr,v) for(auto itr=v.begin();itr!=v.end();++itr)
#define REPIR(itr,v) for(auto itr=v.rbegin();itr!=v.rend();++itr)
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define SORT(v,n) sort(v, v+n)
#define SORTV(v) sort(v.begin(), v.end())
#define ALL(v) v.begin(),v.end()
#define llong long long
#define ll long long
#define INF 999999999
#define SUR 1000000007
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define MP make_pair
#define SV(n,v) {int tmp;for(int i=0;i<n;++i){scanf("%d",&tmp);v.push_back(tmp);}}

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};

using namespace std;

typedef pair<int,int> pii;

struct edge{int u, v; llong cost;};

void init(int par[], int V){
  for(int i = 0; i < V; ++i)
    par[i] = i;
}

int find(int x, int par[]){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = find(par[x], par);
  }
}

void unite(int x, int y, int par[], int rank[]){
  x = find(x, par);
  y = find(y, par);

  if(x == y)
    return;

  if(rank[x] > rank[y]){
    par[y] = x;
  }else{
    par[x] = y;
    if(rank[x] == rank[y])
      rank[y]++;
  }
}

int same(int x, int y, int par[]){
  return find(x, par) == find(y, par);
}

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

llong kruskal(int V, int E, vector<edge> es){
  sort(ALL(es), comp);
  int *par, *rank;

  par = new int[V];
  rank = new int[V];

  init(par, V);
  REP(i,V){
    rank[i] = 0;
  }

  llong res = 0;
  REP(i,E){
    edge e = es[i];
    if(!same(e.u, e.v, par)){
      unite(e.u, e.v, par, rank);
      res += e.cost;
    }
  }
  
  delete[] par;
  delete[] rank;

  return res;
}

int main(){

  int n;
  scanf("%d", &n);

  vector<pii> x, y;
  REP(i,n){
    int tx, ty;
    scanf("%d %d", &tx, &ty);
    x.push_back({tx, i});
    y.push_back({ty, i});
  }

  SORTV(x); SORTV(y);

  vector<edge> v;
  REP(i,n - 1){
    v.push_back({x[i].second, x[i + 1].second, x[i + 1].first - x[i].first});
    v.push_back({y[i].second, y[i + 1].second, y[i + 1].first - y[i].first});
  }

  printf("%lld\n", kruskal(n, (int)v.size(), v));

  return 0;
}

Submission Info

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

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:109:18: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
./Main.cpp:114:29: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &tx, &ty);
                             ^

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 9188 KB
02.txt AC 68 ms 9060 KB
03.txt AC 68 ms 8932 KB
04.txt AC 68 ms 9444 KB
05.txt AC 68 ms 8804 KB
06.txt AC 68 ms 8932 KB
07.txt AC 68 ms 8932 KB
08.txt AC 68 ms 8804 KB
09.txt AC 66 ms 8804 KB
10.txt AC 66 ms 9444 KB
11.txt AC 66 ms 8804 KB
12.txt AC 67 ms 9444 KB
13.txt AC 52 ms 8804 KB
14.txt AC 52 ms 8804 KB
15.txt AC 49 ms 9700 KB
16.txt AC 52 ms 8804 KB
17.txt AC 54 ms 8804 KB
18.txt AC 52 ms 8804 KB
19.txt AC 50 ms 8804 KB
20.txt AC 52 ms 9700 KB
21.txt AC 52 ms 9060 KB
22.txt AC 53 ms 8804 KB
23.txt AC 36 ms 9060 KB
24.txt AC 51 ms 9700 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