C/C++ | 并查集:用于检查一个图上有没有环
没有环的过程分析:
#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++ | 并查集:用于检查一个图上有没有环的更多相关文章
- 【并查集的另一个思考方向】POJ1456
POJ1456 这个题一看好像就是用贪心做啊,一个结构体,拍一下序,vis数组一遍遍扫荡,最后输出值,没错,贪心的确能做出来,而这类题目也能应用并查集,实现得思想也是贪心 #include <i ...
- poj 1182 食物链 并查集的又一个用法
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41584 Accepted: 12090 Descripti ...
- 线段树 or 并查集 (多一个时间截点)
There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...
- D. Restructuring Company 并查集 + 维护一个区间技巧
http://codeforces.com/contest/566/problem/D D. Restructuring Company time limit per test 2 seconds m ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- LeetCode:并查集
并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态 ...
- 【POJ 1182 食物链】并查集
此题按照<挑战程序设计竞赛(第2版)>P89的解法,不容易想到,但想清楚了代码还是比较直观的. 并查集模板(包含了记录高度的rank数组和查询时状态压缩) *; int par[MAX_N ...
- codeforces 1023 D. Array Restoration 并查集
D. Array Restoration time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 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 ...
随机推荐
- Linux 下在后台运行进程:nohup,setsid,& 以及 tmux
参考: Linux 技巧:让进程在后台可靠运行的几种方法 ssh 登录了远程服务器时,如果在前台运行耗时较长的任务, 当 ssh 掉线或关闭窗口时会导致命令停止运行. hup 与 nohup 当用户注 ...
- WPF与DevExpress之——实现类似于安装程序下一步下一步的样式窗体
话不多说先上图 点击下一步 跳转到第二页 项目准备: 1.DevExpress 19/18/17(三个版本都可以) 2.Vs2019 3..Net framework>4.0 项目结构: ...
- day15生成器send方法,递归,匿名函数,max结合匿名工作原理,常用的内置函数
复习 ''' 1.带参装饰器 - 自定义 | wraps def wrap(info) def outer1(func): from functools import wraps @wraps(fun ...
- dvorak键盘布局调整
一站直达: http://www.kaufmann.no/roland/dvorak/
- jmeter _Random函数生成随机数
因对发送邮件接口做压测发现相同数据对服务器的压力很小所以需要每次发送请求都需要不同的参数,所以要对某个字段做随机数 选项中-函数助手对话框
- mysql5.7日志时间与系统时间不一致
在MySQL 5.7.2 新增了 log_timestamps 这个参数,该参数主要是控制 error log.genera log,等等记录日志的显示时间参数 且默认安装后error_log,slo ...
- MySQL学习笔记(上)
在进行SQL注入原理的剖析的时候,对MySQL数据库掌握薄弱,参照菜鸟教程的MySQL教程速刷一遍MySQL 关于MySQL MySQL是最流行的关系型数据库管理系统,在WEB方面MySQL是最好的R ...
- loading 加载工具
loading 加载工具:http://loading.awesomes.cn/
- C# 中File和FileStream的用法
原文:https://blog.csdn.net/qq_41209575/article/details/89178020 1.首先先介绍File类和FileStream文件流 1.1 File类, ...
- ionic -v2版本项目结构
myApp │ config.xml //项目配置文件,包名.名称.minSdkVersion等都在此处配置 │ ionic.config.json │ package.json //项目依 ...