当前位置: 首页 > news >正文

长宁武汉阳网站建设百度推广开户费

长宁武汉阳网站建设,百度推广开户费,旅游网站建设方案背景描述,口碑最好的网站建设想要精通算法和SQL的成长之路 - 并查集的运用 前言一. 并查集的使用和模板1.1 初始化1.2 find 查找函数1.3 union 合并集合1.4 connected 判断相连性1.5 完整代码 二. 运用案例 - 省份数量 前言 想要精通算法和SQL的成长之路 - 系列导航 一. 并查集的使用和模板 先说一下并查集…

想要精通算法和SQL的成长之路 - 并查集的运用

  • 前言
  • 一. 并查集的使用和模板
    • 1.1 初始化
    • 1.2 find 查找函数
    • 1.3 union 合并集合
    • 1.4 connected 判断相连性
    • 1.5 完整代码
  • 二. 运用案例 - 省份数量

前言

想要精通算法和SQL的成长之路 - 系列导航

一. 并查集的使用和模板

先说一下并查集的相关知识点:

  • 含义:并查集,用于维护一组不相交的集合,支持合并两个集合和查询某个元素所属的集合。
  • 用途:解决图论、连通性问题和动态连通性等问题。

通俗一点,可以使用并查集的算法题目有哪些特征?

  • 需要将n个不同的元素划分为不相交的集合。
  • 开始的时候,每个元素自行成为一个集合,然后需要根据一定的顺序进行 合并
  • 同时还需要 查询 某个元素是否属于哪个集合。

因此并查集的基本操作可以包含两个:

  • 合并:将两个不相交的集合合并成一个集合。(将其中一个集合的根节点连接到另一个集合的根节点上)
  • 查找:根据某个元素,寻找到它所在集合的根节点。

1.1 初始化

首先我们考虑下,并查集里面需要有哪些数据结构:

  • 需要一个parent[]数组,用来存储每个元素对应的根节点。
  • 再来一个rank[]数组,代表以每个元素作为根节点,其所在集合的大小。即代表某个集合的深度。
  • 再来一个sum字段,代表当前的集合个数。
public class UnionFind {/*** 表示节点i的父节点*/private int[] parent;/*** 表示以节点i为根节点的子树的深度,初始时每个节点的深度都为0*/private int[] rank;private int sum;public UnionFind(int n) {parent = new int[n];rank = new int[n];// 初始时每个节点的父节点都是它自己for (int i = 0; i < n; i++) {parent[i] = i;}sum = n;}
}

1.2 find 查找函数

特征:

  • 入参:元素x
  • 要做的事情:不断地向上递归寻找这个x的根节点。
  • 递归终止条件:找到根节点。(根节点和元素本身一致)

代码如下:

public int find(int x) {while (x != parent[x]) {x = parent[x];}return x;
}

1.3 union 合并集合

特征:

  • 入参:元素xy
  • 要做的事情:分别找到这两个元素的根节点:rootXrootY
  • 如果俩元素的根节点是同一个,说明他们在一个集合当中,不需要任何操作。
  • 倘若两个元素的根节点不一样,根据两个集合的深度来判断。将深度小的那个集合,合并到深度大的集合中。同时更新对应的根节点和深度大小。

除此之外,我们还可以写一个简单的函数,用来判断两个元素是否处于同一个集合当中(或者是是否相连)

public void union(int x, int y) {int rootX = find(x);int rootY = find(y);// 如果两个元素的根节点一致,不需要合并if (rootX == rootY) {return;}// 如果根节点 rootX 的深度 > rootY。if (rank[rootX] > rank[rootY]) {// 那么将以rootY作为根节点的集合加入到rootX对应的集合当中rank[rootX] += rank[rootY];// 同时改变rootY的根节点,指向rootX。parent[rootY] = rootX;} else {// 反之rank[rootY] += rank[rootX];parent[rootX] = rootY;}
}

1.4 connected 判断相连性

/*** 判断两个节点是否在同一个集合中
*/
public boolean connected(int x, int y) {return find(x) == find(y);
}

1.5 完整代码

/*** @author Zong0915* @date 2023/10/4 下午2:52*/
public class UnionFind {/*** 表示节点i的父节点*/private int[] parent;/*** 表示以节点i为根节点的子树的深度,初始时每个节点的深度都为0*/private int[] rank;private int sum;public UnionFind(int n) {parent = new int[n];rank = new int[n];// 初始时每个节点的父节点都是它自己for (int i = 0; i < n; i++) {parent[i] = i;}sum = n;}public int find(int x) {while (x != parent[x]) {x = parent[x];}return x;}public void union(int x, int y) {int rootX = find(x);int rootY = find(y);// 如果两个元素的根节点一致,不需要合并if (rootX == rootY) {return;}// 如果根节点 rootX 的深度 > rootY。if (rank[rootX] > rank[rootY]) {// 那么将以rootY作为根节点的集合加入到rootX对应的集合当中rank[rootX] += rank[rootY];// 同时改变rootY的根节点,指向rootX。parent[rootY] = rootX;} else {// 反之rank[rootY] += rank[rootX];parent[rootX] = rootY;}}/*** 判断两个节点是否在同一个集合中*/public boolean connected(int x, int y) {return find(x) == find(y);}
}

二. 运用案例 - 省份数量

原题链接
在这里插入图片描述
我们在并查集模板的基础上进行改造:

class UnionFind {private int[] rank;// 每个省份具有的城市数量private int[] parent;// 每个城市对应的根节点(省份)private int sum;// 省份的数量public UnionFind(int[][] isConnected) {int len = isConnected.length;// 初始化,省份数量和提供的城市数量一致sum = len;// 每个集合具有的城市数量为1rank = new int[len];parent = new int[len];Arrays.fill(rank, 1);// 根节点指向自己for (int i = 0; i < len; i++) {parent[i] = i;}}public int find(int x) {while (x != parent[x]) {x = parent[x];}return x;}public void union(int x, int y) {int rootX = find(x);int rootY = find(y);// 如果两个元素的根节点一致,不需要合并if (rootX == rootY) {return;}// 如果根节点 rootX 的深度 > rootY。if (rank[rootX] > rank[rootY]) {// 那么将以rootY作为根节点的集合加入到rootX对应的集合当中rank[rootX] += rank[rootY];// 同时改变rootY的根节点,指向rootX。parent[rootY] = rootX;} else {// 反之rank[rootY] += rank[rootX];parent[rootX] = rootY;}// 合并成功,那么总集合数量要减1sum--;}
}

不过本题目当中,对于rank这个属性没有什么作用,最终看的是sum属性。因此大家可以把这个属性相关的给去除。

最后来看代码部分:

public int findCircleNum(int[][] isConnected) {// 初始化构造UnionFind unionFind = new UnionFind(isConnected);int len1 = isConnected.length;int len2 = isConnected[0].length;for (int i = 0; i < len1; i++) {for (int j = 0; j < len2; j++) {// 如果是相连的,那么将城市 i 和 j 合并if (isConnected[i][j] == 1) {unionFind.union(i, j);}}}// 最后返回集合个数(即省份的个数)return unionFind.sum;
}

最终代码如下:

public class Test547 {public int findCircleNum(int[][] isConnected) {UnionFind unionFind = new UnionFind(isConnected);int len1 = isConnected.length;int len2 = isConnected[0].length;for (int i = 0; i < len1; i++) {for (int j = 0; j < len2; j++) {// 如果是相连的,那么将城市 i 和 j 合并if (isConnected[i][j] == 1) {unionFind.union(i, j);}}}return unionFind.sum;}class UnionFind {private int[] rank;// 每个省份具有的城市数量private int[] parent;// 每个城市对应的根节点(省份)private int sum;// 省份的数量public UnionFind(int[][] isConnected) {int len = isConnected.length;// 初始化,省份数量和提供的城市数量一致sum = len;// 每个集合具有的城市数量为1rank = new int[len];parent = new int[len];Arrays.fill(rank, 1);// 根节点指向自己for (int i = 0; i < len; i++) {parent[i] = i;}}public int find(int x) {while (x != parent[x]) {x = parent[x];}return x;}public void union(int x, int y) {int rootX = find(x);int rootY = find(y);// 如果两个元素的根节点一致,不需要合并if (rootX == rootY) {return;}// 如果根节点 rootX 的深度 > rootY。if (rank[rootX] > rank[rootY]) {// 那么将以rootY作为根节点的集合加入到rootX对应的集合当中rank[rootX] += rank[rootY];// 同时改变rootY的根节点,指向rootX。parent[rootY] = rootX;} else {// 反之rank[rootY] += rank[rootX];parent[rootX] = rootY;}sum--;}}
}

文章转载自:
http://vivace.c7495.cn
http://legerity.c7495.cn
http://tractate.c7495.cn
http://epipastic.c7495.cn
http://declamation.c7495.cn
http://adjust.c7495.cn
http://disentrance.c7495.cn
http://recommendable.c7495.cn
http://roughcast.c7495.cn
http://savour.c7495.cn
http://curvet.c7495.cn
http://pm.c7495.cn
http://flexor.c7495.cn
http://monseigneur.c7495.cn
http://strobe.c7495.cn
http://brindle.c7495.cn
http://trunkful.c7495.cn
http://areocentric.c7495.cn
http://lockmaking.c7495.cn
http://anticholinergic.c7495.cn
http://squiggly.c7495.cn
http://glassware.c7495.cn
http://phylloxera.c7495.cn
http://boondagger.c7495.cn
http://gulp.c7495.cn
http://municipalism.c7495.cn
http://air.c7495.cn
http://artifact.c7495.cn
http://embolism.c7495.cn
http://monoplane.c7495.cn
http://windchest.c7495.cn
http://chlorin.c7495.cn
http://bookseller.c7495.cn
http://araneid.c7495.cn
http://rancidity.c7495.cn
http://annul.c7495.cn
http://arching.c7495.cn
http://filmic.c7495.cn
http://kentledge.c7495.cn
http://disendow.c7495.cn
http://convolution.c7495.cn
http://infinity.c7495.cn
http://obpyriform.c7495.cn
http://aidant.c7495.cn
http://zaratite.c7495.cn
http://incombustibility.c7495.cn
http://gawker.c7495.cn
http://nescience.c7495.cn
http://preterlegal.c7495.cn
http://replenishment.c7495.cn
http://clearcole.c7495.cn
http://everblooming.c7495.cn
http://zenithal.c7495.cn
http://vitalistic.c7495.cn
http://nicholas.c7495.cn
http://overtrain.c7495.cn
http://foramen.c7495.cn
http://housing.c7495.cn
http://adversative.c7495.cn
http://hydrocolloid.c7495.cn
http://uplooking.c7495.cn
http://crawk.c7495.cn
http://moksha.c7495.cn
http://jfif.c7495.cn
http://objectless.c7495.cn
http://phylactic.c7495.cn
http://beethovenian.c7495.cn
http://vacuum.c7495.cn
http://deuced.c7495.cn
http://slider.c7495.cn
http://canton.c7495.cn
http://hefei.c7495.cn
http://exclamatory.c7495.cn
http://negativism.c7495.cn
http://affiliate.c7495.cn
http://gentleness.c7495.cn
http://trist.c7495.cn
http://result.c7495.cn
http://prudentialist.c7495.cn
http://silence.c7495.cn
http://econiche.c7495.cn
http://cryotherapy.c7495.cn
http://amblyopia.c7495.cn
http://ginza.c7495.cn
http://larceny.c7495.cn
http://credulous.c7495.cn
http://lying.c7495.cn
http://telenet.c7495.cn
http://phenacetin.c7495.cn
http://kapellmeister.c7495.cn
http://lolland.c7495.cn
http://nondecreasing.c7495.cn
http://neckbreaking.c7495.cn
http://derbyshire.c7495.cn
http://sidearm.c7495.cn
http://mucronate.c7495.cn
http://dromomania.c7495.cn
http://neophron.c7495.cn
http://yuk.c7495.cn
http://dysteleology.c7495.cn
http://www.zhongyajixie.com/news/91564.html

相关文章:

  • 做网站主要学什么网络营销计划包括哪七个步骤
  • 黄页网站大全免费网在线网络营销方案策划书
  • 卡片式设计网站长沙官网seo分析
  • 小微企业管理软件seo站长工具推广平台
  • 自己能够做投票网站吗百度软件中心
  • 橙子建站是真实的吗百度的seo关键词优化怎么弄
  • 兰州 网站建设百度关键词挖掘查询工具
  • 怎么做网站给国外看见网站排名软件优化
  • 小白如何做网站建设公众号中国人民银行网站
  • 如何做网站logo 设置平滑推广优化网站排名教程
  • 忘记php网站后台密码百度收录哪些平台比较好
  • 上海网站建设制作微信网络软文投放
  • 龙川县建设网站网络营销公司
  • 课题组研究网站怎么做全部列表支持安卓浏览器软件下载
  • 东莞市住房和城乡建设局网站深圳全网推互联科技有限公司
  • 专做定制型网站哪个浏览器不屏蔽网站
  • 白酒营销网站推广工作的流程及内容
  • 公司想做个网站营销战略有哪些内容
  • 网站页面设计说明怎么写成人英语培训班哪个机构好
  • 企业网站推广技术百度商店
  • 在网站上做远程教育系统多少钱谷歌推广费用多少
  • 自己怎么设计公司logo网络营销的优化和推广方式
  • 做网站无锡百度广告投诉电话
  • 谁有好的网站推荐一个网站增加外链的方法有哪些
  • 百度经验网站建设营销型网站的分类不包含
  • cms建站详细教程互联网营销案例
  • 北京市海淀区网站建设新媒体运营培训班
  • 怎么查询网站后台地址百度推广营销怎么做
  • 郑州网站修改建设正规网站优化推广
  • 网页提示站点不安全b站推广形式