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

企业网站开发报价表百度ai搜索引擎

企业网站开发报价表,百度ai搜索引擎,制作图片视频的软件有哪些,wordpress软件商城介绍 英文名:union find set 作用:合并集合,查询集合 合并:将有直接关系的顶点放在一个集合里面 查找:查询某个顶点所属的集合 集合的标志:用祖先点的标号作为每个集合的标识 案例 如果说将下图的集合2合并…
介绍

        英文名:union find set

        作用:合并集合,查询集合

        合并:将有直接关系的顶点放在一个集合里面

        查找:查询某个顶点所属的集合

        集合的标志:用祖先点的标号作为每个集合的标识

案例 

          如果说将下图的集合2合并至集合1

         那么应该让3的祖先为1,即:

 

实现流程

        1)对所有顶点标号,其祖先节点默认为自己

        2)合并:以两个点中祖先标号小的作为新祖先进行合并

        3)查找:查找点的祖先,并更新查找路径点的祖先

Java代码实现
//顶点类
public class Vertex {public Integer id;public Vertex(Integer id){this.id=id;}
}//并查集的操作类
public class UnionFindSetOperator {private List<Vertex>vertexs;    //所有顶点private List<List<Vertex>>connectVertexs;    //所有有直接关系的顶点private Map<Vertex,Vertex>ancestor;public UnionFindSetOperator(List<Vertex> vertexs, List<List<Vertex>> connectVertexs) {this.vertexs = vertexs;this.connectVertexs = connectVertexs;this.ancestor=new HashMap<>();//将所有顶点的祖先初始化默认设置为自己for(int i=0;i< vertexs.size();i++){ancestor.put(vertexs.get(i),vertexs.get(i));}//合并所有直接相连的顶点for(int i=0;i< connectVertexs.size();i++){Union(connectVertexs.get(i).get(0),connectVertexs.get(i).get(1));}}public void Union(Vertex vertex1,Vertex vertex2){Vertex ancestor1=Find(vertex1);Vertex ancestor2=Find(vertex2);//选取两个祖先id小的作为新祖先if(ancestor1.id<ancestor2.id)ancestor.put(vertex2,ancestor1);elseancestor.put(vertex1,ancestor2);}public Vertex Find(Vertex vertex){if(ancestor.get(vertex)!=vertex){//寻找该节点的祖先Vertex ancestorVertex=Find(ancestor.get(vertex));//如果祖先发生了变化,则更新记录新的祖先if(ancestorVertex!=ancestor.get(vertex))ancestor.put(vertex,ancestorVertex);}//返回其祖先return ancestor.get(vertex);}
}
 
测试函数
//测试函数
public class Main {/*并查集:简述:对集合进行分类合并,方便后续查找实现步骤:1)对所有顶点标号,其祖先节点默认为自己2)合并:以两个点中祖先标号小的作为新祖先进行合并3)查找:查找点的祖先,并更新查找路径点的祖先*/public static void main(String[] args) {//扫描器Scanner scanner=new Scanner(System.in);//存放顶点List<Vertex>vertexs=new LinkedList<>();//有直接关联的顶点List<List<Vertex>>connectVertexs=new LinkedList<>();System.out.println("请输入顶点的数量:");Integer vexcnt= scanner.nextInt();System.out.println("请输入这些顶点的编号");for(int i=0;i<vexcnt;i++){vertexs.add(new Vertex(scanner.nextInt()));}System.out.println("请输入有关系顶点的对数:");Integer connectVertexCnt= scanner.nextInt();System.out.println("请输入有关系顶点的编号:");for(int i=0;i<connectVertexCnt;i++){Integer id1= scanner.nextInt();Integer id2= scanner.nextInt();//获取这两个顶点Vertex v1=null;Vertex v2=null;for(int j=0;j<vertexs.size();j++){if(vertexs.get(j).id==id1)v1=vertexs.get(j);if(vertexs.get(j).id==id2)v2=vertexs.get(j);}LinkedList<Vertex>conVertexs=new LinkedList<>();conVertexs.add(v1);conVertexs.add(v2);connectVertexs.add(conVertexs);}//用并查集的操作类实现这些点的分类和查找UnionFindSetOperator ufs=new UnionFindSetOperator(vertexs,connectVertexs);//查找每一个点的祖先for(int i=0;i<vertexs.size();i++){Vertex ancestor=ufs.Find(vertexs.get(i));System.out.println("顶点:"+vertexs.get(i).id+"的祖先id为:"+ancestor.id);}}
}
自测

        测试模型:

        测试结果:

 

http://www.dinnco.com/news/9220.html

相关文章:

  • 哪些网站可以下载视频推广方案流程
  • 建设开发有限公司网站什么是优化
  • 做网站去哪里下载素材建网站需要什么
  • wordpress被扫描网站如何优化排名软件
  • 阿里云服务器做电影网站下载手机百度最新版
  • 公司网站建设大概多少钱百度搜索推广的定义
  • 东莞网站建设 家具百度搜索推广登录入口
  • nginx wordpress 二级目录奉化首页的关键词优化
  • 普通人怎么样做网站百度系优化
  • 自己买域名可以做网站吗云优客seo排名公司
  • 网站模板下载 网盘新东方英语培训机构官网
  • 东莞网站建设价格价格网站制作报价表
  • 有没有那个的网站最近一周新闻大事
  • robots 网站地图阿里巴巴关键词排名优化
  • 网站功能定位分析成人馆店精准引流怎么推广
  • 做网站怎么挣钱怎么制作个人网页
  • axure做购物网站企业网站模板设计
  • 网站开发报价表模板自己做网站
  • 网站建设电话咨询长治网站seo
  • 门户网站系统设计国际足联世界排名
  • 沧州手机网站建设福州seo视频
  • 微网站对比搜狗推广助手
  • 视频网站开发周期长沙seo培训班
  • 国外免费wordpress主题站长工具seo综合查询引流
  • 临沂网站建设设计线上推广的方式
  • 信息大全关键词优化工具有哪些
  • 用wordpress建站效果怎么样电话销售外呼系统软件
  • 企业做电商网站有哪些百度网盘下载慢怎么解决
  • 网站网页能自己做吗做个电商平台要多少钱
  • 成都网站建设多少钱新媒体运营岗位职责