Submission #2116664


Source Code Expand

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

using ll = long long;
using P  = pair<ll,ll>;

struct Edge{
    using Cost = ll;
    ll to;
    Cost cost;
    Edge(ll t, Cost c) : to(t), cost(c){} 
};
using Edges = vector<Edge>;
using Graph = vector<vector<Edge>>;

bool operator < (const Edge &e,const Edge &f){
    return e.cost != f.cost ? e.cost > f.cost:
        e.to > f.to;
}

ll MST(const Graph &g, int r=0){
    ll n=g.size();
    ll total=0;

    vector<bool> visited(n);
    priority_queue<Edge> q;
    q.push(Edge(r,0));
    while(!q.empty()){
        Edge e=q.top();q.pop();
        if(visited[e.to]) continue;
        //cout<<e.to<<" "<<e.cost<<endl;
        total += e.cost;
        visited[e.to] = true;
        for(int i=0;i<(int)g[e.to].size();i++){
            if(!visited[g[e.to][i].to]) q.push(g[e.to][i]);
        }
    }
    return total;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

    int n;cin>>n;
    Graph g(n);
    vector<P> x,y;
    for(int i=0;i<n;i++){
        ll xtmp,ytmp;cin>>xtmp>>ytmp;
        x.push_back(P(xtmp,i));
        y.push_back(P(ytmp,i));
    }
    sort(x.begin(),x.end());
    sort(y.begin(),y.end());
    for(int i=1;i<n;i++){
        ll s=x[i-1].second,t=x[i].second;
        ll c=x[i].first-x[i-1].first;
        g[s].push_back(Edge(t,c));
        g[t].push_back(Edge(s,c));
        //cout<<s<<" "<<t<<" "<<c<<endl;
        
        s=y[i-1].second;t=y[i].second;
        c=y[i].first-y[i-1].first;
        g[s].push_back(Edge(t,c));
        g[t].push_back(Edge(s,c));
        //cout<<s<<" "<<t<<" "<<c<<endl;
    }
    //cout<<endl;
    cout<<MST(g)<<endl;
}

Submission Info

Submission Time
Task D - Built?
User zaki_
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1697 Byte
Status AC
Exec Time 120 ms
Memory 17380 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 112 ms 17380 KB
02.txt AC 119 ms 17380 KB
03.txt AC 120 ms 17380 KB
04.txt AC 116 ms 17380 KB
05.txt AC 116 ms 17380 KB
06.txt AC 120 ms 17380 KB
07.txt AC 117 ms 17380 KB
08.txt AC 114 ms 17380 KB
09.txt AC 113 ms 17380 KB
10.txt AC 109 ms 17380 KB
11.txt AC 115 ms 17380 KB
12.txt AC 115 ms 17380 KB
13.txt AC 97 ms 17380 KB
14.txt AC 86 ms 17380 KB
15.txt AC 61 ms 13540 KB
16.txt AC 65 ms 13668 KB
17.txt AC 65 ms 13668 KB
18.txt AC 65 ms 13540 KB
19.txt AC 62 ms 13540 KB
20.txt AC 86 ms 16612 KB
21.txt AC 76 ms 14052 KB
22.txt AC 70 ms 13668 KB
23.txt AC 42 ms 13540 KB
24.txt AC 86 ms 14436 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