博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
并查集
阅读量:4320 次
发布时间:2019-06-06

本文共 843 字,大约阅读时间需要 2 分钟。

/*并查集*/#include
int *a;int *sz;int count; //the number of connected component//union two connected components with weightsvoid union_two_points(int p, int q){ int i = root(p); int j = root(q); if(i == j) return; if(sz[i] < sz[j]) { a[i] = j; sz[j] += sz[i]; } else { a[j] = i; sz[i] += sz[j]; } count--;}//test if p and q are connectedint connected(int p, int q){ return root[p] == root[q];}//find the root pointint root(int p){ while(p != a[p]) { p = a[p]; } return p;}int main(){ int T; printf("Please input the number of points:"); scanf("%d",&T); count = T; a = (int*)malloc(sizeof(int)*T); sz = (int*)malloc(sizeof(int)*T); memset(sz,1,T*sizeof(int)); //set the size array //initial the array for(int i=0;i

 

转载于:https://www.cnblogs.com/dzy521/p/9552893.html

你可能感兴趣的文章
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
学习进度
查看>>
“此人不存在”
查看>>