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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. #define dbg(x) cerr << #x << " = " << x <<endl
  8. using namespace std;
  9. typedef long long ll;
  10. typedef double db;
  11. typedef pair<int,int> pii;
  12. template<typename T>inline T _min(T A,T B){return A<B?A:B;}
  13. template<typename T>inline T _max(T A,T B){return A>B?A:B;}
  14. template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,):;}
  15. template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,):;}
  16. template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
  17. template<typename T>inline T read(T&x){
  18. x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
  19. while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
  20. }
  21. const int N=+;
  22. struct thxorz{
  23. int u,v,w;
  24. thxorz(int u=,int v=,int w=):u(u),v(v),w(w){}
  25. inline bool operator <(const thxorz&A)const{return w<A.w;}
  26. }e[N<<];
  27. int n,tot;
  28. struct stothx{int x,y,id;}A[N];
  29. inline bool cmp1(stothx a,stothx b){return a.x<b.x;}
  30. inline bool cmp2(stothx a,stothx b){return a.y<b.y;}
  31. int anc[N];
  32. ll ans;
  33. inline int get_anc(int x){return anc[x]==x?x:anc[x]=get_anc(anc[x]);}
  34.  
  35. int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
  36. read(n);
  37. for(register int i=;i<=n;++i)read(A[i].x),read(A[i].y),A[i].id=i;
  38. sort(A+,A+n+,cmp1);
  39. for(register int i=;i<n;++i)e[++tot]=thxorz(A[i].id,A[i+].id,A[i+].x-A[i].x);
  40. sort(A+,A+n+,cmp2);
  41. for(register int i=;i<n;++i)e[++tot]=thxorz(A[i].id,A[i+].id,A[i+].y-A[i].y);
  42. sort(e+,e+tot+);
  43. for(register int i=;i<=n;++i)anc[i]=i;
  44. 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];
  45. printf("%lld\n",ans);
  46. return ;
  47. }

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. springboot整合es客户端操作elasticsearch(二)

    在上章节中整合elasticsearch客户端出现版本问题进行了处理,这章来进行springboot整合得操作 环境:elaticsearch6.2.1,springboot 2.1.8 客户端版本采 ...

  2. 第十四周课程总结&实验报告

    简单记事本的实现 实验源码 主类 package test1; import javax.swing.JFrame; import javax.swing.JTextArea; public clas ...

  3. POJ1988 Cube Stacking 【并查集】

    题目链接:http://poj.org/problem?id=1988 这题是教练在ACM算法课上讲的一道题,当时有地方没想明白,现在彻底弄懂了. 题目大意:n代表有n个石头,M a, b代表将a石头 ...

  4. autossh

    LDAP:用old HK login SSH转发:/root/.nat_rules/opay-new root@ldap opay-new]# cat maria1-dw-60000 #!/bin/b ...

  5. python列表和if语句的简单结合

    将列表所有元素打印出来 cars = ['toyota', 'honda', 'mazda', 'nissan', 'mitsubishi', 'subaru', 'suzuki', 'isuzu'] ...

  6. 【转帖】K8s-yaml的使用及命令

    K8s-yaml的使用及命令 https://www.cnblogs.com/fuyuteng/p/9460534.html YAML配置文件管理对象 对象管理: # 创建deployment资源 k ...

  7. Git基本理解

    1.版本控制 Git 是一个分布式版本控制系统 (Distributed Version Control System - DVCS). 所谓版本控制,意思就是在文件的修改历程中保留修改历史,让你可以 ...

  8. Linux-echo:打印彩色输出

    脚本可以使用转义序列在终端中生成彩色文本 文本颜色是由对应的色彩码来描述的.其中包括: 重置=0,黑色=30,红色=31,绿色=32, 黄色=33,蓝色=34,洋红=35,青色=36,白色=37. 要 ...

  9. idea的配置文件------application.properties和application.yml

    当application.yml 和 application.properties 两个文件同时存在的时候,application.properties的优先级是高于application.yml的, ...

  10. IE浏览器中判断IE版本

    <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...