没有环的过程分析:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define VERTICES 6
#define LINE 5
using namespace std; /*
parent:数组解决并查集合并问题
VERTICES:设定的顶点数
LINE:设定的边数
*/
void initialise(int *parent)
{
//用于parent数组的初始化
int i;
for(i=0;i<VERTICES;i++)
{
parent[i]=-1;
}
}
int find_root(int x,int parent[])
{
//用来查找根节点
int x_root = x;
while(parent[x_root] != -1)
{
x_root = parent[x_root];
}
return x_root; }
int union_vertices(int x,int y,int parent[])
{
//用于合并两个集合 。返回0:合并成功,返回1:合并失败
int x_root=find_root(x,parent);
int y_root=find_root(y,parent);
printf("x:当前数是%d,他的parent是:%d\n",x,x_root);
printf("y:当前数是%d,他的parent是:%d\n",y,y_root); if(x_root==y_root)
{
return 0;
}
else
{
parent[x_root]=y_root;//将x连到y上
return 1;
} }
int main()
{
int parent[VERTICES]={0};
initialise(parent);
int edges[LINE][2] = {
{0,1},{1,2},{1,3},
{2,4},{2,5}
};
for(int i=0;i<LINE;++i)
{
int x=edges[i][0];
int y=edges[i][1];
if(union_vertices(x,y,parent)==0)
{
printf("存在环");
exit(0);
}
}
printf("没有环");
return 0;
}

最后一次执行了“parent[x_root]=y_root;”,所以4的parent变成了5

存在环的过程分析:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define VERTICES 6
#define LINE 6
using namespace std; /*
parent:数组解决并查集合并问题
VERTICES:设定的顶点数
LINE:设定的边数
*/
void initialise(int *parent)
{
//用于parent数组的初始化
int i;
for(i=0;i<VERTICES;i++)
{
parent[i]=-1;
}
}
int find_root(int x,int parent[])
{
//用来查找根节点
int x_root = x;
while(parent[x_root] != -1)
{
x_root = parent[x_root];
}
return x_root; }
int union_vertices(int x,int y,int parent[])
{
//用于合并两个集合 。返回0:合并成功,返回1:合并失败
int x_root=find_root(x,parent);
int y_root=find_root(y,parent);
printf("x:当前数是%d,他的parent是:%d\n",x,x_root);
printf("y:当前数是%d,他的parent是:%d\n",y,y_root); if(x_root==y_root)
{
return 0;
}
else
{
parent[x_root]=y_root;//将x连到y上
return 1;
} }
int main()
{
int parent[VERTICES]={0};
initialise(parent);
int edges[LINE][2] = {
{0,1},{1,2},{1,3},
{2,4},{2,5},{3,4}
};
for(int i=0;i<LINE;++i)
{
int x=edges[i][0];
int y=edges[i][1];
if(union_vertices(x,y,parent)==0)
{
printf("存在环");
exit(0);
}
}
printf("没有环");
return 0;
}

最后一次没有执行“parent[x_root]=y_root;”

路径压缩:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define VERTICES 6
#define LINE 6
using namespace std; void initialise(int *parent,int rank[])
{ int i;
for(i=0;i<VERTICES;i++)
{
parent[i]=-1;
rank[i]=0;
}
}
int find_root(int x,int parent[])
{ int x_root = x;
while(parent[x_root] != -1)
{
x_root = parent[x_root];
}
return x_root; }
int union_vertices(int x,int y,int parent[],int rank[])
{ int x_root=find_root(x,parent);
int y_root=find_root(y,parent);
printf("x:当前数是%d,他的parent是:%d,他的rank是:%d\n",x,x_root,rank[x]);
printf("y:当前数是%d,他的parent是:%d,他的rank是:%d\n",y,y_root,rank[y]); if(x_root==y_root)
{
return 0;
}
else
{
if(rank[x_root] > rank[y_root])
{
parent[y_root]=x_root;
}
else if(rank[y_root]>rank[x_root])
{
parent[x_root]=y_root;
}
else
{
parent[x_root]=y_root;
rank[y_root]++;
}
return 1;
} }
int main()
{
int parent[VERTICES]={0};
int rank[VERTICES]={0};
initialise(parent,rank);
int edges[LINE][2] = {
{0,1},{1,2},{1,3},
{2,4},{2,5},{3,4}
};
for(int i=0;i<LINE;++i)
{
int x=edges[i][0];
int y=edges[i][1];
if(union_vertices(x,y,parent,rank)==0)
{
printf("存在环");
exit(0);
}
}
printf("没有环");
return 0;
}

C/C++ | 并查集:用于检查一个图上有没有环的更多相关文章

  1. 【并查集的另一个思考方向】POJ1456

    POJ1456 这个题一看好像就是用贪心做啊,一个结构体,拍一下序,vis数组一遍遍扫荡,最后输出值,没错,贪心的确能做出来,而这类题目也能应用并查集,实现得思想也是贪心 #include <i ...

  2. poj 1182 食物链 并查集的又一个用法

    食物链   Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 41584   Accepted: 12090 Descripti ...

  3. 线段树 or 并查集 (多一个时间截点)

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  4. D. Restructuring Company 并查集 + 维护一个区间技巧

    http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...

  5. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  6. LeetCode:并查集

    并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态 ...

  7. 【POJ 1182 食物链】并查集

    此题按照<挑战程序设计竞赛(第2版)>P89的解法,不容易想到,但想清楚了代码还是比较直观的. 并查集模板(包含了记录高度的rank数组和查询时状态压缩) *; int par[MAX_N ...

  8. codeforces 1023 D. Array Restoration 并查集

    D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

随机推荐

  1. JAVA在页面查看或下载服务器上的日志

    1.配置 FileUtils类所需jar包的maven地址 <dependency> <groupId>commons-io</groupId> <artif ...

  2. 个人对BFC的见解

    BFC:块级格式化上下文,它是指一个独立的块级渲染区域,只有Block-level BOX参与,该区域拥有一套渲染规则来约束块级盒子的布局,且与区域外部无关. BFC的生成 既然上文提到BFC是一块渲 ...

  3. iBatis框架之配置文件之注意点之总结

    1.配置文件sqlMap.xml中需要注意的点 比如: <?xml version="1.0" encoding="UTF-8" ?> <!D ...

  4. 自己挖的坑自己填--docker创建实例出现Waiting for SSH to be available…

    在之前使用Docker for Windows Installer.exe直接安装,通过docker-machine-driver-vmwareworkstation.exe实现docker和VM的共 ...

  5. Spring MVC-学习笔记(2)DispatcherServlet、@Controller、@RequestMapping、处理方法参数类型、可返回类型、Model、ModelMap、ModelAndView

    1.前端控制器org.springframework.web.servlet.DispatcherServlet 所有的请求驱动都围绕这个DispatcherServlet来分派请求.springMV ...

  6. El 表达式和 Jstl 标签库

    El 表达式学习 1. 什么是 EL 表达式 全称:Expression Language,一种写法非常简介的表达式.语法简单易懂,便于使用.表达式语言的灵感来自于 ECMAScript 和XPath ...

  7. CF912E Prime Gift题解(搜索+二分答案)

    CF912E Prime Gift题解(搜索+二分答案) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1314956 洛谷题目链接 $     $ CF题目 ...

  8. 自定义django中间件

    自定义中间件 第一步:在根目录创建路径Middle/m1.py(注意如果是python2的话Middle下要有__init__.py文件,不然会报找不到模块错误) m1.py的内容: # -*- co ...

  9. NancyFx框架之检测任务管理器

    先建一个空的项目和之前的NancyFx系列一样的步骤 然后建三个文件夹Models,Module,Views 然后分别安装一下组件 jQuery Microsoft.AspNet.SignalR Mi ...

  10. BigDecimal 的用法

    1.初始化 BigDecimal discount=new BigDecimal(0.9); BigDecimal discount=new BigDecimal(200); 2.加减乘除 加法 ad ...