这题和某道最短路题神似。对于任意点对,将他们连边,不如将他们分别沿$x,y$轴方向上点按顺序连起来,这样不仅可能多连通一些点,也花费更低,所以按照最短路那题的连边方式跑一个kruskal就行了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define dbg(x) cerr << #x << " = " << x <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+;
struct thxorz{
int u,v,w;
thxorz(int u=,int v=,int w=):u(u),v(v),w(w){}
inline bool operator <(const thxorz&A)const{return w<A.w;}
}e[N<<];
int n,tot;
struct stothx{int x,y,id;}A[N];
inline bool cmp1(stothx a,stothx b){return a.x<b.x;}
inline bool cmp2(stothx a,stothx b){return a.y<b.y;}
int anc[N];
ll ans;
inline int get_anc(int x){return anc[x]==x?x:anc[x]=get_anc(anc[x]);} int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
read(n);
for(register int i=;i<=n;++i)read(A[i].x),read(A[i].y),A[i].id=i;
sort(A+,A+n+,cmp1);
for(register int i=;i<n;++i)e[++tot]=thxorz(A[i].id,A[i+].id,A[i+].x-A[i].x);
sort(A+,A+n+,cmp2);
for(register int i=;i<n;++i)e[++tot]=thxorz(A[i].id,A[i+].id,A[i+].y-A[i].y);
sort(e+,e+tot+);
for(register int i=;i<=n;++i)anc[i]=i;
for(register int i=;i<=tot;++i)if(get_anc(e[i].u)^get_anc(e[i].v))ans+=e[i].w,anc[anc[e[i].u]]=anc[e[i].v];
printf("%lld\n",ans);
return ;
}

ABC065D Built[最小生成树]的更多相关文章

  1. Built(最小生成树+构图离散化)

    个人心得:看了题目很明确,最小生成树,但是但是周赛卡住了,因为10W的点若一个一个找出距离很明显内存和时间都炸了, 静下心来,画了下图,仔细一想,任意一个点都只会在她左右俩边选择建立联系,那么我们只要 ...

  2. timus 1982 Electrification Plan(最小生成树)

    Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The govern ...

  3. POJ 1751 Highways (最小生成树)

    Highways 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/G Description The island nation ...

  4. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  5. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

  6. hdu1102 Constructing Roads (简单最小生成树Prim算法)

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  7. POJ 2485:Highways(最小生成树&amp;&amp;prim)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21628   Accepted: 9970 Descrip ...

  8. POJ2031Building a Space Station (最小生成树之prim)

    Problem Description You are a member of the space station engineering team, and are assigned a task ...

  9. ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

随机推荐

  1. co源码

    co模块整体包括三部分 对于几种参数类型的判断,主要判断是否object,array,promise,generator,generatorFunction这几种; 将几种不同的参数类型转换为prom ...

  2. Vue:不同页面之间的传递参数--params

    在嵌套vue-router情况下,不同页面之间传递参数可以通过params实现.而params传参分为两种情况: 1.参数在url中显示 首先你要确定自己要传的参数,并在控制路由的文件中的Router ...

  3. SpringBoot集成MybatisPlus报错

    SpringBoot集成MybatisPlus报错 启动的时候总是报如下错误: java.lang.annotation.AnnotationFormatError: Invalid default: ...

  4. $listeners 在vue中的使用 --初学

    事件回传之 $listeners 组件由下向上回传事件 <!doctype html><html lang="en"> <head> <m ...

  5. THUSC2016

    补退选 Luogu LOJ BZOJ 比较裸. 建一棵Trie树,记录一下每个节点的\(sum\)表示经过该点的字符串个数,每次暴力插入.删除. 同时每个节点维护一个vector,记录一下这个点的\( ...

  6. localStorage 杂记

    localStorage html5标准 Web 存储现在的主流浏览器,包括IE 8+.Chrome 4+.Firefox 3.5+.Opera 10.5+.Safari 4+.iPhone 2+.A ...

  7. 2019牛客暑期多校训练营(第二场) - B - Eddy Walker 2 - BM算法

    参考于: https://www.luogu.org/problemnew/solution/P4723 shadowice1984 (太难) https://www.cnblogs.com/zhgy ...

  8. Web前端开发JavaScript基础

    JavaScript 一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型,它的解释器被称为JavaScript引擎,是浏览器的一部分,并且是被广泛用于客户端的脚本语言,JavaS ...

  9. Codeforces 1238G. Adilbek and the Watering System

    传送门 最关键的想法就是每个位置一定用的是当前能用的最便宜的水,因为到后面可能有更便宜的 然后其他还没用上的水我们也留着,假装此时已经买了,但是如果发现后面有更优的再反悔也不迟 每相邻两个朋友之间我们 ...

  10. mqtt协议实现 java服务端推送功能(一)安装

    最近有个新需求,需要通过java服务端把信息推送到mqtt服务器上,安卓和ios端从mqtt服务器上获取信息实现推送. 1. 本地需要安装Mosquitto服务器  http://mosquitto. ...