Submission #11644546


Source Code Expand

// Date: 2020-04-07

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

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define IO freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout)
#define FIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define D(x) cout << #x << " = " << x << endl;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)x.size())
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {LL a=1; while(y){ if(y&1) a=a*x%MOD; x=x*x%MOD; y>>=1; } return a;}

int n, p[N];
pii a[N], b[N];

struct edge {
    int u, v, w;
    bool operator < (const edge& r) const { return w < r.w; }
} E[N];

int Find(int x) {
    return x == p[x] ? x : p[x] = Find(p[x]);
}

int main() {
    FIO;
    cin >> n;
    rep(i, 1, n) {
        p[i] = i;
        int u, v;
        cin >> u >> v;
        a[i] = mk(u, i);
        b[i] = mk(v, i);
    }
    sort(a+1, a+1+n);
    sort(b+1, b+1+n);
    int tot = 0;
    rep(i, 2, n) E[++tot] = {a[i].se, a[i-1].se, a[i].fi-a[i-1].fi}, E[++tot] = {b[i].se, b[i-1].se, b[i].fi-b[i-1].fi};
    sort(E+1, E+1+tot);
    LL ans = 0;
    rep(i, 1, tot) {
        int u = E[i].u, v = E[i].v;
        int fu = Find(u), fv = Find(v);
        if(fu != fv) p[fu] = fv, ans += E[i].w;
    }
    cout << ans;

    return 0;
}

Submission Info

Submission Time
Task D - Built?
User Gladinum
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1843 Byte
Status AC
Exec Time 56 ms
Memory 6400 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 55 ms 6400 KB
02.txt AC 55 ms 6400 KB
03.txt AC 55 ms 6400 KB
04.txt AC 55 ms 6400 KB
05.txt AC 55 ms 6400 KB
06.txt AC 55 ms 6400 KB
07.txt AC 56 ms 6400 KB
08.txt AC 55 ms 6400 KB
09.txt AC 53 ms 6400 KB
10.txt AC 53 ms 6400 KB
11.txt AC 52 ms 6400 KB
12.txt AC 52 ms 6400 KB
13.txt AC 38 ms 6400 KB
14.txt AC 38 ms 6400 KB
15.txt AC 36 ms 6400 KB
16.txt AC 39 ms 6400 KB
17.txt AC 40 ms 6400 KB
18.txt AC 39 ms 6400 KB
19.txt AC 38 ms 6400 KB
20.txt AC 40 ms 6400 KB
21.txt AC 39 ms 6400 KB
22.txt AC 40 ms 6400 KB
23.txt AC 24 ms 6400 KB
24.txt AC 41 ms 6400 KB
25.txt AC 2 ms 4352 KB
26.txt AC 2 ms 4352 KB
s1.txt AC 2 ms 4352 KB
s2.txt AC 2 ms 4352 KB