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

wordpress go上海seo优化bwyseo

wordpress go,上海seo优化bwyseo,中国建设网官方,上海公司注册代理公司文章目录 1. 并查集介绍2. 并查集的实现2.1 实现逻辑2.2 isSameSet方法2.3 union方法(小挂大优化)2.4 find方法(路径压缩优化) 3. 并查集模板4. 并查集习题4.1 情侣牵手4.2 相似字符串组 1. 并查集介绍 定义: 并查集是一种树型的数据结构,用于处理一些不…

文章目录

    • 1. 并查集介绍
    • 2. 并查集的实现
      • 2.1 实现逻辑
      • 2.2 isSameSet方法
      • 2.3 union方法(小挂大优化)
      • 2.4 find方法(路径压缩优化)
    • 3. 并查集模板
    • 4. 并查集习题
      • 4.1 情侣牵手
      • 4.2 相似字符串组

1. 并查集介绍

定义:
并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题(即所谓的并、查)。比如说,我们可以用并查集来判断一个森林中有几棵树、某个节点是否属于某棵树等
并查集的常见的方法:

方法作用
int find (int)作用就是查找一个元素所在大集合的代表元素, 返回这个元素
boolean isSameSet (int, int)判断传入的两个元素是不是同属一个大集合, 返回T/F
void union (int, int)合并传入的两个元素所代表的大集团(注意不仅仅是这两个元素)

并查集的时间复杂的要求就是实现上述的操作的时间复杂度都是O(1)
下面是关于并查集的一些常见的操作的图示
在这里插入图片描述

2. 并查集的实现

2.1 实现逻辑

不论是哈希表的机构还是list的顺序结构或者是其他的常见的数据结构, 都不可以做到时间复杂度是O(1)的这个指标, 我们直接介绍实现的方式 --> 通过一个father数组以及size数组
关于这两个数组的含义:

数组含义
father下标i代表的是元素的编号, father[i]代表的是他的父亲节点
size下标i代表的是元素的编号, size[i]代表的是这个节点的孩子节点的个数(包括本身)

在这里插入图片描述
初态就是这个样子, 每一个元素的父亲节点都是其本身, 也就是说每一个节点本身就是其所在集合的代表节点, 然后这个集合的大小就是1
下面我们执行操作
step1 : union(a, b)
step2 : union(c, a)
下面是图示(图解一下操作1, 操作2其实是同理的)
在这里插入图片描述
上面的图解也说明了很多问题, 我们的树形结构的挂载的方式是, 小挂大(小的树挂到大树上)
此时进行了union操作之后的逻辑结构就是左下角所示, 此时我们 {a,b} 共属于一个集合, 进行find操作的时候, find(a) 的结果是 b, find(b) 的结果也是 b, 此时size数组中a的值不会再使用了, 因为这时a不可能是领袖节点了, 也就是说这个数据是脏数据…

2.2 isSameSet方法

其实正常来说我们的isSameSet方法和union方法都需要调用find方法, 但是find方法中的路径压缩的技巧是比较重要的, 所以我们单独拎出来放后面说(这里假设已经实现好了), 实现也是比较简单的, 只需要找到这两个元素的代表领袖节点看是不是一个就可以了

	//isSameSet方法private static boolean isSameSet(int a, int b){return find(a) == find(b);}

2.3 union方法(小挂大优化)

解释一下小挂大概念, 在算法导论这本书中说到的是一种秩的概念, 本质上也是为了降低树(集团)的高度所做出的努力, 但这个不是特别必要的…, 也就是在两大集团合并的时候, 小集团(小数目的节点)要依附大集团而存在, 也就是合并的时候, 小集团要挂在大集团上面, 这样可以从一定程度上降低树的高度
代码实现如下

	//union方法private static void union(int a, int b){int fa = find(a);int fb = find(b);if(fa != fb){sets--;if(size[fa] >= size[fb]){father[fb] = fa;size[fa] += size[fb];}else{father[fa] = fb;size[fb] += size[fa];}}}

2.4 find方法(路径压缩优化)

上面的union的小挂大优化, 其实不是特别必要的, 但是我们find方法中的路径压缩是一定要完成的, 如果没有路径压缩的话, 我们的时间复杂度的指标就不会是O(1)
路径压缩指的就是, 在find方法找到父亲节点的时候, 同时把我们的沿途所有节点的父亲节点都改为找到的父亲节点, 以便于操作的时候不用遍历一个长链去寻找父亲节点, 图解如下
在这里插入图片描述
假设我们执行find(a)操作, 就会如图所示把我们的沿途的所有节点的父亲节点都改为领袖节点e
我们借助的是stack栈结构, 或者是递归(其实就是系统栈)实现的

private static final int MAX_CP = 31;private static final int[] father = new int[MAX_CP];private static final int[] size = new int[MAX_CP];private static final int[] stack = new int[MAX_CP];//find方法(路径压缩的迭代实现)private static int find1(int a){int sz = 0;while(father[a] != a){stack[sz++] = a;a = father[a];}while(sz > 0){father[stack[--sz]] = a;}return father[a];}//find方法(路径压缩的递归实现)private static int find(int a){if(father[a] != a){father[a] = find(father[a]);}return father[a];}

3. 并查集模板

上面就是我们关于并查集最基本的分析, 我们提供几个测试链接测试一下

牛客并查集模板

//并查集的基本实现方式
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.io.OutputStreamWriter;
import java.io.IOException;public class Main {private static final int MAXN = 1000001;private static final int[] father = new int[MAXN];private static final int[] size = new int[MAXN];private static final int[] stack = new int[MAXN];private static int cnt = 0;private static void build(int sz) {cnt = sz;for (int i = 0; i <= cnt; i++) {father[i] = i;size[i] = 1;}}private static int find(int n) {//下面就是扁平化(路径压缩的处理技巧)int capacity = 0;while (father[n] != n) {stack[capacity++] = n;n = father[n];}//开始改变沿途节点的指向while (capacity > 0) {father[stack[--capacity]] = n;}return father[n];}private static boolean isSameSet(int a, int b) {return find(a) == find(b);}private static void union(int a, int b) {//下面的设计就是小挂大的思想int fa = find(a);int fb = find(b);if (fa != fb) {if (size[fa] >= size[fb]) {father[fb] = fa;size[fa] += size[fb];} else {father[fa] = fb;size[fb] += size[fa];}}}//我们使用的是高效率的io工具(使用的其实就是一种缓存的技术)public static void main(String[] args) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StreamTokenizer in = new StreamTokenizer(br);PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));while (in.nextToken() != StreamTokenizer.TT_EOF) {int n = (int)in.nval;build(n);in.nextToken();int m = (int)in.nval;for (int i = 0; i < m; i++) {in.nextToken();int op = (int)in.nval;in.nextToken();int n1 = (int)in.nval;in.nextToken();int n2 = (int)in.nval;if (op == 1) {out.println(isSameSet(n1, n2) ? "Yes" : "No");} else {union(n1, n2);}}}out.flush();out.close();br.close();}
}

洛谷并查集模板

//并查集的基本实现方式
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.io.OutputStreamWriter;
import java.io.IOException;public class Main {private static final int MAXN = 100001;private static final int[] father = new int[MAXN];private static final int[] size = new int[MAXN];private static final int[] stack = new int[MAXN];private static int cnt = 0;private static void build(int sz){cnt = sz;for(int i = 0; i <= cnt; i++){father[i] = i;size[i] = 1;}}private static int find(int n){//下面就是扁平化(路径压缩的处理技巧)int capacity = 0;while(father[n] != n){stack[capacity++] = n;n = father[n];}//开始改变沿途节点的指向while(capacity > 0){father[stack[--capacity]] = n;}return father[n];}private static boolean isSameSet(int a, int b){return find(a) == find(b);}private static void union(int a, int b){//下面的设计就是小挂大的思想int fa = find(a);int fb = find(b);if(fa != fb){if(size[fa] >= size[fb]){father[fb] = fa;size[fa] += size[fb];}else{father[fa] = fb;size[fb] += size[fa];}}}//我们使用的是高效率的io工具(使用的其实就是一种缓存的技术)public static void main(String[] args) throws IOException{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));StreamTokenizer in = new StreamTokenizer(br);PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));while(in.nextToken() != StreamTokenizer.TT_EOF){int n = (int)in.nval;build(n);in.nextToken();int m = (int)in.nval;for(int i = 0; i < m; i++){in.nextToken();int op = (int)in.nval;in.nextToken();int n1 = (int)in.nval;in.nextToken();int n2 = (int)in.nval;if(op == 2){out.println(isSameSet(n1, n2) ? "Y" : "N");}else{union(n1, n2);}}}out.flush();out.close();br.close();}
}

4. 并查集习题

4.1 情侣牵手

leetcode765.情侣牵手题目链接
在这里插入图片描述

//本题的前置知识可能是置换环(这一题的并查集的思路尤其不好想)
class Solution {
//核心点的分析就是如果一个集合里面有k对情侣, 那么我们至少需要交换 k - 1 次private static final int MAX_CP = 31;private static final int[] father = new int[MAX_CP];private static final int[] size = new int[MAX_CP];private static final int[] stack = new int[MAX_CP];private static int sets = 0;//初始化并查集private static void build(int n){sets = n;for (int i = 0; i < n; i++) {father[i] = i;size[i] = 1;}}//find方法(路径压缩的实现)//find方法(路径压缩的递归实现)private static int find(int a){if(father[a] != a){father[a] = find(father[a]);}return father[a];}//isSameSet方法private static boolean isSameSet(int a, int b){return find(a) == find(b);}//union方法private static void union(int a, int b){int fa = find(a);int fb = find(b);if(fa != fb){sets--;if(size[fa] >= size[fb]){father[fb] = fa;size[fa] += size[fb];}else{father[fa] = fb;size[fb] += size[fa];}}}public int minSwapsCouples(int[] row) {int cpN = row.length / 2;build(cpN);for(int i = 0; i < row.length; i += 2){union(row[i] / 2, row[i + 1] / 2);}return cpN - sets;}
}

4.2 相似字符串组

leetcode839.相似字符串组
在这里插入图片描述

//简单的并查集的应用
class Solution {private static final int MAXN = 301;private static final int[] father = new int[MAXN];private static final int[] size = new int[MAXN];private static final int[] stack = new int[MAXN];private static int sets = 0;//初始化并查集的方式private static void build(int n){sets = n;for(int i = 0; i < n; i++){father[i] = i;size[i] = 1;}}//find方法private static int find(int a){int sz = 0;while(father[a] != a){stack[sz++] = a;a = father[a];}while(sz > 0){father[stack[--sz]] = a;}return father[a];}//isSameSet方法 private static boolean isSameSet(int a, int b){return find(a) == find(b);}//union方法private static void union(int a, int b){int fa = find(a);int fb = find(b);if(fa != fb){sets--;if(size[fa] >= size[fb]){size[fa] += size[fb];father[fb] = fa;}else{size[fb] += size[fa];father[fa] = fb;}}}public int numSimilarGroups(String[] strs) {int n = strs.length;int m = strs[0].length();build(n);for(int i = 0; i < n; i++){for(int j = i + 1; j < n; j++){if (find(i) != find(j)) {int diff = 0;for (int k = 0; k < m && diff < 3; k++) {if (strs[i].charAt(k) != strs[j].charAt(k)) {diff++;}}if (diff == 0 || diff == 2) {union(i, j);}}}}return sets;}
}

文章转载自:
http://physicist.Lnnc.cn
http://burnoose.Lnnc.cn
http://donjon.Lnnc.cn
http://transurethral.Lnnc.cn
http://ngc.Lnnc.cn
http://calfhood.Lnnc.cn
http://synaesthesia.Lnnc.cn
http://flannelled.Lnnc.cn
http://dekagram.Lnnc.cn
http://fabrikoid.Lnnc.cn
http://intercalary.Lnnc.cn
http://unfulfilment.Lnnc.cn
http://estragon.Lnnc.cn
http://cotemporary.Lnnc.cn
http://journalistic.Lnnc.cn
http://picturegoer.Lnnc.cn
http://nattierblue.Lnnc.cn
http://gracious.Lnnc.cn
http://affray.Lnnc.cn
http://position.Lnnc.cn
http://proteolytic.Lnnc.cn
http://listee.Lnnc.cn
http://tritiated.Lnnc.cn
http://trenail.Lnnc.cn
http://pergunnah.Lnnc.cn
http://psychologise.Lnnc.cn
http://unsheltered.Lnnc.cn
http://berascal.Lnnc.cn
http://lethality.Lnnc.cn
http://ironer.Lnnc.cn
http://streptomycete.Lnnc.cn
http://naw.Lnnc.cn
http://portability.Lnnc.cn
http://destructor.Lnnc.cn
http://australasian.Lnnc.cn
http://favoringly.Lnnc.cn
http://parian.Lnnc.cn
http://silkscreen.Lnnc.cn
http://consecutively.Lnnc.cn
http://maglev.Lnnc.cn
http://mugearite.Lnnc.cn
http://exuviation.Lnnc.cn
http://wec.Lnnc.cn
http://corded.Lnnc.cn
http://avowedly.Lnnc.cn
http://kindergarener.Lnnc.cn
http://bootlick.Lnnc.cn
http://nyet.Lnnc.cn
http://hydroborate.Lnnc.cn
http://fewtrils.Lnnc.cn
http://gadbee.Lnnc.cn
http://chancery.Lnnc.cn
http://fleurette.Lnnc.cn
http://dedicator.Lnnc.cn
http://intercalate.Lnnc.cn
http://scandisk.Lnnc.cn
http://cetological.Lnnc.cn
http://costermonger.Lnnc.cn
http://outhouse.Lnnc.cn
http://unhumanize.Lnnc.cn
http://sergeant.Lnnc.cn
http://pangola.Lnnc.cn
http://subconscious.Lnnc.cn
http://mesorrhine.Lnnc.cn
http://discerptible.Lnnc.cn
http://algologist.Lnnc.cn
http://noncondensing.Lnnc.cn
http://garrotter.Lnnc.cn
http://sporadosiderite.Lnnc.cn
http://knickered.Lnnc.cn
http://mouthpart.Lnnc.cn
http://swiss.Lnnc.cn
http://ovoid.Lnnc.cn
http://cosmologic.Lnnc.cn
http://quantise.Lnnc.cn
http://castigation.Lnnc.cn
http://piacular.Lnnc.cn
http://canto.Lnnc.cn
http://miscreant.Lnnc.cn
http://isc.Lnnc.cn
http://largish.Lnnc.cn
http://ssl.Lnnc.cn
http://oogenesis.Lnnc.cn
http://windbound.Lnnc.cn
http://healingly.Lnnc.cn
http://alpinist.Lnnc.cn
http://egotrip.Lnnc.cn
http://explanation.Lnnc.cn
http://apologist.Lnnc.cn
http://alastrim.Lnnc.cn
http://sclerite.Lnnc.cn
http://adultery.Lnnc.cn
http://transpositional.Lnnc.cn
http://troubleshooter.Lnnc.cn
http://jingoistically.Lnnc.cn
http://slanguage.Lnnc.cn
http://magnetofluiddynamic.Lnnc.cn
http://hyperlink.Lnnc.cn
http://dsc.Lnnc.cn
http://siluroid.Lnnc.cn
http://www.dt0577.cn/news/112859.html

相关文章:

  • 个人网站怎么推广桂林网站设计制作
  • 网站页面设计公司惠州百度推广排名
  • 那个网站ppt做的比较好职业培训网络平台
  • 做电焊加工的网站动态网站设计
  • 电子商务网站建设品牌兰州seo外包公司
  • 电子商务网站预算app推广方法及技巧
  • 惠州网站建设l优选蓝速科技网盟推广
  • 网站流量统计代码可以用javascript实现么做网店自己怎么去推广
  • 网页设计鉴赏湖南长沙seo教育
  • 如何查看网站的空间商公司运营策划营销
  • 做网站公司需要什么职位人工智能培训心得
  • 曹县 做网站的公司沈阳seo整站优化
  • 中牟网站建设网络营销品牌
  • 怎么做网站广告网络营销的宏观环境
  • 做网站客户制作网站需要多少费用
  • wordpress基地seo自然优化排名
  • 教人做网站的视频企业推广方案
  • 婚庆网站建设百度seo点击工具
  • 怎么连接网站的虚拟主机如何搭建一个自己的网站
  • 黑龙江恒泰建设集团网站上海网络营销seo
  • 建设vip网站相关视频seo技术培训茂名
  • 河南哪里网站建设公司最近疫情最新消息
  • 做网销的网站百度电话号码查询平台
  • 自己做的网站只能打开一个链接百度安装到桌面
  • 金湖建设局网站营销模式都有哪些
  • wordpress用户二级域名什么叫seo网络推广
  • 直播一级a做爰片免费网站品牌营销策划有限公司
  • 南宁网站建设公司怎么接单磁力宅
  • 织梦生成网站地图充电宝seo关键词优化
  • 南宁百度seo网站优化购物网站页面设计